1. Use for loop traversal, for indexed arrays
$arr = Array (1,2,3,4,5,6,7,8,9);
For ($i =0;count ($arr); $i + +); {
echo $arr [$i]. " <br> ";
}
2. Using foreach to iterate through an array can be used for indexes and associations
Index
$arr = Array (1,2,3,4,5,6,7,8,9);
foreach ($arr as $v) {
echo $v. " <br> ";
}
Association
$arr = Array ("One" =>1, "=>2", "three" =>3, "four" =>4);
foreach ($arr as $v) {
echo $v. " <br> ";
}
foreach ($arr as $k = = $v) {
echo $v. " <br> ";
echo $k. " <br> ";
echo "{$k}--{$v}<br>";
}
3. Use the list () and each () method to iterate through an array
Each method is called multiple times to remove all elements from the array.
List method
List ($a, $b) = Array ("One", 1);
echo $a;
Two mates array traversal
$arr = Array ("One" =>1, "=>2", "three" =>3, "four" =>4);
while (list ($k, $v) =each ($arr)) {
echo "{$k}--{$v}<br>";
}
4. Iterating through the array using pointers
$arr = Array ("One" =>1, "=>2", "three" =>3, "four" =>4);
Echo current ($arr); Remove the value of the element that the current pointer points to
echo Key ($arr); Takes out the index of the current pointer to the element
Next ($arr); Adjust the pointer back
Prev ($arr); Adjust the pointer up
End ($arr); Adjust the pointer to the last
Reset ($arr); Reset the pointer
while (Next ($arr)) {
Echo current ($arr);
echo Key ($arr);
}
do{
Echo current ($arr);
echo Key ($arr);
}while (Next ($arr))
PHP Traversal Array