Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList in php

Tags:

Are there any data structures in php other than array. Is it possible to create a data structure such as an ArrayList? If so please provide some references or some kind of implementation.

like image 740
Cooper Avatar asked Dec 31 '10 13:12

Cooper


People also ask

Does PHP have ArrayList?

The closest PHP likeness to the ArrayList class from Java is the ArrayObject class. The method names are different, but the functionality between the two is fairly close. but it doesn't have contains method.

Are there lists in PHP?

The list function in PHP is an inbuilt function which is used to assign the array values to multiple variables at a time while execution. Like array (), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.

What are the 3 types of PHP arrays?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

What is the use of list in PHP?

The list() function is used to assign values to a list of variables in one operation. Note: Prior to PHP 7.1, this function only worked on numerical arrays.


2 Answers

Everything you need to know about arrays can be found in the documentation.

All of the available functions for arrays are listed in the functions reference.

Some notes:

  • Arrays were originally the only datastructure in PHP. That is why they are so flexible. They were meant to be used as stack, queue, array, list, hash table and so forth. Later on PHP introduced the spl Datastructures.
  • Unlike Java, PHP is not a pure OO language. An array itself has no built in methods you can apply. This has to be done via "normal" functions.
  • Arrays have no fixed size. They expand and shrink automatically.

In the following I tried to list the PHP alternatives for the most common ArrayList methods:

  • add(element) is basically just appending via $array[] = $element. The new value gets the next free numeric index (this is the preferred way). You can also use array_push($array, $element).
  • addAll(ArrayList): array_merge($array1, $array2) in a way. Be careful when merging associative arrays. Values for the same keys will be overwritten.
  • clone(): As arrays are not objects, you "clone" an array by just assigning it to another variable:

        $a = array(1,2,3);     $b = $a; 
  • contains(element): in_array($element, $array)

  • get(index): Like in most languages: $val = $array[index];
  • indexOf(element): array_keys($array, $element) with the value you search for as second parameter
  • isEmpty(): empty($array)
  • remove(index): Its unset($array[index])
  • remove(value) with value: You have to get the keys first (see indexOf), iterate over they keys and use unset.
  • size(): count($array)
like image 111
Felix Kling Avatar answered Oct 06 '22 08:10

Felix Kling


I tried to implement, here's some simple code:

class ArrayList {  private $list = array();   public function Add($obj) {     array_push($this->list, $obj); }  public function Remove($key) {     if(array_key_exists($key, $this->list))     {         unset($this->list[$key]);     } }  public function Size() {     return count($this->list); }  public function IsEmpty() {     return empty($this->list); }  public function GetObj($key) {     if(array_key_exists($key, $this->list))     {         return $this->list[$key];     }     else     {         return NULL;     } }  public function GetKey($obj) {     $arrKeys = array_keys($this->list, $obj);      if(empty($arrKeys))         {         return -1;     }     else     {         return $arrKeys[0];     } }  } 
like image 37
Carlos H Avatar answered Oct 06 '22 08:10

Carlos H