Arrays are sets of data that can be defined in a PHP Script. Arrays can contain other arrays inside of them without any restriction (hence building multidimensional arrays). Arrays can be referred to as tables or hashes.
Arrays can be created in two ways. The first involves using the function array. The second involves using square brackets.
In the ''array'' function method, you create an array in the scheme of: $foo = bar() For example, to set up the array to make the keys sequential numbers (Example: "0, 1, 2, 3"), you use:
  $foobar = array($foo, $bar);
This would produce the array like this: 
  $foobar[0] = $foo;
  $foobar[1] = $bar;
It is also possible to define the key value:
 $foobar = array('foo' => $foo, 'bar' => $bar);
This would set the array like this: 
  $foobar['foo'] = $foo;
  $foobar['bar'] = $bar;
The square brackets method allows you to set up by directly setting the values. For example, to make $foobar[1] = $foo, all you need to do is:
 $foobar[1] = $foo;
The same applies for setting the key value:
 $foobar['foo'] = $foo;
{{Code:PHPHTML
|This example sets and prints arrays.
|
"Toyota","type"=>"Celica","colour"=>"black","manufactured"=>"1991");
  $array2 = array("Toyota","Celica","black","1991");
  $array3 = array("name"=>"Toyota","Celica","colour"=>"black","1991");
  print_r($array);
  print_r($array2);
  print_r($array3);
?>
|
  Array
  (
    [name] => Toyota
    [type] => Celica
    [colour] => black
    [manufactured] => 1991
  )
  Array
  (
    [0] => Toyota
    [1] => Celica
    [2] => black
    [3] => 1991
  )
  Array
  (
    [name] => Toyota
    [0] => Celica
    [colour] => black
    [1] => 1991
  )
|Array ( [name] => Toyota [type] => Celica [colour] => black [manufactured] => 1991 ) Array ( [0] => Toyota [1] => Celica [2] => black [3] => 1991 ) Array ( [name] => Toyota [0] => Celica [colour] => black [1] => 1991 ) 
|
The following example will output the identical text as Example #1:
{{Code:PHPHTML
|Using the ''Example #1'' and Example #2 above, now you can try and use arrays the same way as normal variables:
|
| Manufacturer: Toyota
  Brand: Celica
  Colour: black
  Year Manufactured: 1991
|
  Manufacturer: Toyota
  Brand: Celica
  Colour: black
  Year Manufactured: 1991
|
Elements in an array can also be an array, allowing for multidimensional arrays.
An example, in accordance with the motoring examples above, is:
 array("make" => "Toyota","colour" => "Green","year" => 1999,"engine_cc" => 1998),
  "car2" => array("make" => "BMW","colour" => "RED","year" => 2005,"engine_cc" => 2400),
  "car3" => array("make" => "Renault","colour" => "White","year" => 1993,"engine_cc" => 1395),
);
?>
In this example, if you were to use:
";
  echo "$cars['car3']['engine_cc']";
?>
The output would be:
 Toyota
 1395
There are dozens of array manipulation functions. Before implementing your own, make sure it doesn't already exist as a PHP function in [http://php.net/manual/en/ref.array.php Array functions (PHP manual entry)].
Examples:
$array = array("name"=>"Toyota", "type"=>"Celica", "colour"=>"black", "manufactured"=>"1991");
array_multisort($array, SORT_ASC);
var_dump($array);
// array(4) { ["manufactured"]=> string(4) "1991" ["type"]=> string(6) "Celica" ["name"]=> string(6) "Toyota" ["colour"]=> string(5) "black" }
// The upper cases are sorted before the lowercases.
arsort($array);
var_dump($array);
// array(4) { ["colour"]=> string(5) "black" ["name"]=> string(6) "Toyota" ["type"]=> string(6) "Celica" ["manufactured"]=> string(4) "1991" }
asort($array);
var_dump($array);
// array(4) { ["manufactured"]=> string(4) "1991" ["type"]=> string(6) "Celica" ["name"]=> string(6) "Toyota" ["colour"]=> string(5) "black" }
sort($array);
var_dump($array);
// array(4) { [0]=> string(4) "1991" [1]=> string(6) "Celica" [2]=> string(6) "Toyota" [3]=> string(5) "black" }
In various circumstances, you will need to visit every array element and perform a task upon it.
The simplest and the most widely used method for this is the foreach operator that loops through the whole array and works individually with each key/item couple. If a more complex way of traversing the array is needed, the following functions operate using the internal array pointer:
Another possibility is defining a function and applying it to each array element via one of the following functions: