Home >Backend Development >PHP Tutorial >PHP code for traversing arrays, three methods
$people = array("Peter", "Joe", "Glenn", "Cleveland");
$people1 = array("Peter", "Joe", "asd"=>"Glenn" , "Cleveland");
// 7.list(variable 1, variable 2, variable 3); Generally used with each to traverse the array.
// The list function uses the elements in the array to assign values to a set of variables.
// This function is only used for numerically indexed arrays, and assumes that numerical indexes start from 0.
list($a1,$a2,$a3)=$people1;//Assign the array people1 to the list
echo $a1.$a2.$a3;//Output the values in the list
echo "
" ;
// 6.each(array1); Generate an array consisting of the key name and key value of the element pointed to by the current internal pointer of the array,
// and move the internal pointer forward.
print_r (each($people));
print_r (each($people));
reset($people);//Reset the pointer
//Give the key and value in people1 to list
while ( list( $key, $val) = each($people1) )
{
echo "$key => $val
";
}
for( $i=0;$iecho $people[$i]."
";
}
foreach( $people as $v ){
echo $v."
";
}Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the code for traversing arrays in PHP, three methods, including aspects. I hope it will be helpful to friends who are interested in PHP tutorials.