Array in PHP is used as advanced data type of ordered map that associates values to keys. In its simplest forms, an array can contain a flat list of keys and values. However, the array values can be other arrays for unlimited layers in depth. When arrays are set as values of a parent array, a two-dimensional, three-dimensional or even multidimensional nested arrays are created.
Access an array is simple if the array does not contain too many elements, where the code can directly the value of the array by specifying its key name in the following format of syntax: $arr_expression[‘$key’].
Handling and accessing the data in nested arrays variable can be tricky. PHP has a “foreach” construct, which only works on arrays, that gives an easy way to iterate over arrays. Here’s some example codes of which nested arrays (keys and values) can be accessed.
The syntax of foreach construct is as follow:
foreach ($array_expression as $value) { statement }
foreach ($array_expression as $key => $value) { statement }
The first form loops over the array given by $array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one so that on the next loop, the next element is accessed.
The second form does the same thing, except that the current element’s key will be assigned to the variable $key on each loop.
The foreach syntax above is also similar to the following while construct:
while (list(, $value) = each($array_expression)) { statement }
while (list($key, $value) = each($array_expression)) { statement }
Even for loop can be used to process and loop through all elements of arrays with the following syntax:
$count = sizeof($arr_expression); for ($i = 0; $i < $count; $i++) { $value = $arr_express[$i]; statement }
for ($i = 0, $item = ''; false != ($value = $arr_expression[$i]); $i++) { statement }
Nested loop of “foreach” can also be used to access multidimensional arrays. Here’s an example array and the way to access its value data. For example:
foreach ($array_expression as $arr_value) { foreach ($array_value as $value) { statement } }
Here’s an example code to access an array:
$contents = array( "website_1" => array("name" => "My Digital Life", "url" => "http://www.mydigitallife.net", "favorite" => "yes"), "website_2" => array("name" => "Tip and Trick", "url" => "http://www.tipandtrick.net", "favorite" => "yes") );
To retrieve the data, programmers can specify the keys that lead to the value directly with the following syntax, which will print the “My Digital Life”.
echo $contents['website_1']['name'];
However, the syntax above becomes unpractical when dealing with large arrays, or when the name of the keys and values changed dynamically. In this case, the “foreach” function can be used to access an array recursively.
foreach ($contests as $key => $list) { echo "Website No.: " . $key . "\n"; echo "Name: " . $list['name'] . "\n"; echo "URL: " . $list['url'] . "\n"; }
The output will be:
Website No.: website_1
Name: My Digital Life
URL: http://www.mydigitallife.net/
Website No.: website_2
Name: Tip and Trick
URL: http://www.tipandtrick.net/