Accessing All Elements of Numerical Arrays
Looping through numerical arrays can most easily be done using foreach because in each iteration of the loop, the current element in the array is automatically written in a variable. Looping Through an Array with foreach (foreach-n.php)
<?php
$a = array('I', 'II', 'III', 'IV');
foreach ($a as $element) {
echo htmlspecialchars($element) . '<br />';
}
?>
Alternatively, a for loop can also be used. The first array element has the index ; the number of array indices can be retrieved using the count() function. Looping Through an Array with for (for-n.php)
<?php
$a = array('I', 'II', 'III', 'IV');
for ($i = 0; $i < count($a); $i++) {
echo htmlspecialchars($a[$i]) . '<br />';
}
?>
Both ways are equally good (or bad); though, usually, using
foreach
is the much more
Looping Through an Array with each() (each-n.php)
<?php
$a = array('I', 'II', 'III', 'IV');
while ($element = each($a)) {
echo htmlspecialchars($element['value']) .
'<br />'; //or: $element[1]
}
?>
The output of the three listings is always the same, of course. |
Accessing All Elements of Associative Arrays
When using an associative array and wanting to access all data in it, the keys are also of relevance. For this, the
foreach
loop must also provide a variable
Looping Through an Associative Array with foreach (foreach-a.php)
<?php
foreach ($array as $key => $value) = array('one'
=> 'I', 'two' => 'II', 'three' => 'III', 'four'
=> 'IV');
foreach ($array as $key => $value) {
echo htmlspecialchars("$key: $value") . '<br />';
}
?>
Using
count()
is possible:
count()
returns the number of values in the array, not the number of elements. Looping through all array elements with
for
is not
Looping Through an Array with each() (each-a.php)
<?php
$a = array('one' => 'I', 'two' => 'II', 'three' =>
'III', 'four' => 'IV');
while ($element = each($a)) {
echo htmlspecialchars($element['key'] . ': ' .
$element['value']) . '<br />';
//or: $element[0] / $element[1]
}
?>
|
Accessing All Array Elements in Nested Arrays
Nested arrays can be printed really easily by using print_r() . Take a look at the output of the listing in Figure 2.1. Printing a nested array with print_r() (print_r.php)
<pre>
<?php
$a = array(
'Roman' =>
array('one' => 'I', 'two' => 'II', 'three' =>
'III', 'four' => 'IV'),
'Arabic' =>
array('one' => '1', 'two' => '2', 'three' =>
'3', 'four' => '4')
);
print_r($a);
?>
</pre>
Figure 2.1. Printing array contents with print_r() .
However, the output of the
Printing a Nested Array Using a Recursive Function (printNestedArray.php)
<?php
function printNestedArray($a) {
echo '<blockquote>';
foreach ($a as $key => $value) {
echo htmlspecialchars("$key: ");
if (is_array($value)) {
printNestedArray($value);
} else {
echo htmlspecialchars($value) . '<br />';
}
}
echo '</blockquote>';
}
$arr = array(
'Roman' =>
array('one' => 'I', 'two' => 'II', 'three' =>
'III', 'four' => 'IV'),
'Arabic' =>
array('one' => '1', 'two' => '2', 'three' =>
'3', 'four' => '4')
);
printNestedArray($arr);
?>
Figure 2.2. Printing array contents using a recursive function.
|