0

Well I've thought of having ArrayList in PHP because it was extremely useful in PHP, and saving a lot of time.

I thought it'd be cool to have in PHP, so I did make an arrayList class in PHP:

Class ArrayList
{
    public $arrayList;

    function __construct()
    {
        $this->arrayList = array();
    }

    public function add($element)
    {
        $this->arrayList[] = $element;
    }

    public function remove($index)
    {
        if (is_numeric($index)) {
            unset($this->arrayList[$index]);
        }
    }

    public function get($index)
    {
        return $this->arrayList[$index];
    }
}

Now, I noticed I need an list type more of a hashmap, so I can get items by keys. Let's say I need to get mysql db name, so Ill just do $data->hashmap->get("db_name"). This will return the database name value.

Is there a way to do this?

2 Answers 2

4

PHP has built-in data types that do what you want:

  • A "hashmap" is an associative array
  • An "ArrayList" is simply an array

Example:

$my_hash_map = array('x' => 5, 'y' => 10);
$my_hash_map['x'] + $my_hash_map['y'] // => 15

$my_array_list = array();
$my_array_list[] = 5;
$my_array_list[0] // => 5

See Arrays in the PHP documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

In PHP arrays can have string keys. Also you could use stdClass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.