PHP Programming/Arrays

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.
Syntax.
Arrays can be created in two ways. The first involves using the function array. The second involves using square brackets.
The "array" function method.
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' =&gt; $foo, 'bar' =&gt; $bar);
This would set the array like this: 
 $foobar['foo'] = $foo;
 $foobar['bar'] = $bar;
The square brackets method.
The square brackets method allows you to set up by directly setting the values. For example, to make codice_1, all you need to do is:
 $foobar[1] = $foo;
The same applies for setting the key value:
 $foobar['foo'] = $foo;
Examples of arrays.
Example #2.
The following example will output the identical text as Example #1:
&lt;?php
 $array['name'] = "Toyota";
 $array['type'] = "Celica";
 $array['colour'] = "black";
 $array['manufactured'] = "1991";
 $array2[] = "Toyota";
 $array2[] = "Celica";
 $array2[] = "black";
 $array2[] = "1991";
 $array3['name'] = "Toyota";
 $array3[] = "Celica";
 $array3['colour'] = "black";
 $array3[] = "1991";
 print_r($array);
 print_r($array2);
 print_r($array3);
Multidimensional arrays.
Elements in an array can also be an array, allowing for multidimensional arrays.
An example, in accordance with the motoring examples above, is:
&lt;?php
$cars = array(
 "car1" =&gt; array("make" =&gt; "Toyota","colour" =&gt; "Green","year" =&gt; 1999,"engine_cc" =&gt; 1998),
 "car2" =&gt; array("make" =&gt; "BMW","colour" =&gt; "RED","year" =&gt; 2005,"engine_cc" =&gt; 2400),
 "car3" =&gt; array("make" =&gt; "Renault","colour" =&gt; "White","year" =&gt; 1993,"engine_cc" =&gt; 1395),
?&gt;
In this example, if you were to use:
&lt;?php
 echo "$cars['car1']['make']&lt;br&gt;";
 echo "$cars['car3']['engine_cc']";
The output would be:
 Toyota
 1395
Array functions.
There are dozens of array manipulation functions. Before implementing your own, make sure it doesn't already exist as a PHP function in Array functions (PHP manual entry).
Sorting.
Examples:
$array = array("name"=&gt;"Toyota", "type"=&gt;"Celica", "colour"=&gt;"black", "manufactured"=&gt;"1991");
array_multisort($array, SORT_ASC);
var_dump($array);
// The upper cases are sorted before the lowercases.
arsort($array);
var_dump($array);
asort($array);
var_dump($array);
sort($array);
var_dump($array);
Array traversal.
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 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:
&lt;?php
// Using an array's iterator to print its values in reverse order
$my_array = array('a', 'b', 'c');
end($my_array);
while ($i = current($my_array)) {
 echo $i."\n";
 prev($my_array);
?&gt;
Another possibility is defining a function and applying it to each array element via one of the following functions: