/* Array traversal
*
* 1. Use the for statement to loop through the array
* 1. Other languages (only this way)
* 2. This method is not the preferred method in PHP
* 3. The array must be an index array, and the subscripts must be consecutive.
* (index array subscripts can be discontinuous, arrays and associative arrays, these two cannot be traversed)
*
* 2. Use the foreach statement to loop through the array
* foreacho(array variable as Variable value) {
* // Loop body
* }
* 1. The number of loops is determined by the number of elements in the array
* 2. Each loop will assign the elements in the array to Later variables
*
* foreach (array variable as subscript variable => value variable) {
* }
*
*
* 3.while() list() each() combines a loop to traverse the array
*
* each() function:
* 1. Requires an array as a parameter
* 2. Returns an array
* 3. Returns The returned array has four subscripts (fixed): 0, 1, key, and value.
* 0 and the key subscript are the keys of the current parameter array element.
* 1 and the value subscript are the keys of the current parameter array element. Value
* 4. By default, the current element is the first element
* 5. Each time it is executed, the current element will be moved backward
* 6. If this function is executed again at the last element, then Return false
* list() function:
* 1. list()=array(); an array needs to be assigned to this function
* 2. The number of elements in the array must be the same as list( ) The number of parameters in the function is the same
* 3. Each element value in the array will be assigned to each parameter in the list() function, and list() will convert each parameter into a variable
* 4.list () can only accept index arrays
* 5. Assign values to parameters in the order of index subscripts
*
*
*
*/
//for statement traverses the array
$user=array(1,"zhangsan",40,"nan");
for($i=0;$i<4;$i++)
{
echo"$user[ {$i}]=".$user[$i]."
";
}
//Use foreach
$user=array(1,"zhangsan",40," nan");
foreach($user as $val)//$val is a custom variable
{
echo $val."
";//The output has nothing to do with the subscript
}
foreach($user as $key=>$val)//$val $key are all custom variables
{
echo $key."=====>". $val."
";
}
//foreach traverses multi-dimensional arrays
$info=array(
"user"=>array(
//$user[ 0]
array(1, "zansan", 10, "nan"),
//$user[1][1]
array(2, "lisi", 20, "nv") , //$user[1]
//$user[2]
array(3, "wangwu", 30, "nan")
),
"score"=>array (
array(1, 100, 90, 80),
array(2, 99, 88, 11),
array(3, 10, 50, 88)
),
"connect"=>array(
array(1, '110', 'aaa@bbb.com'),
array(2, '120', 'bbb@ccc.com'),
array(3, '119', 'ccc@ddd.com')
)
);
foreach($info as $tableName=>$table)
{
echo '
';
echo ''.$tableName.'
caption>';
foreach($table as $row)
{
echo '';
foreach($row as $col)
{
echo ' '.$col.' | ';
}
echo '
';
}
echo '
';
}
//Usage of each()
$user=array("id"=>1,"name"=>"zhangsan","age"=>10, "sex"=>"nan");
$a=each($user);//Array ( [1] => 1 [value] => 1 [0] => id [key ] => id ) Default is the value of the first element
print_r($a);
$b=each($user);
print_r($b);//Array ( [1 ] => zhangsan [value] => zhangsan [0] => name [key] => name ) Each time it is executed, traverse backwards one
$c=each($user);
print_r($c);//Array ( [1] => 10 [value] => 10 [0] => age [key] => age )
$d=each($user) ;
print_r($d);//Array ( [1] => nan [value] => nan [0] => sex [key] => sex )
$e=each ($user);
var_dump($e);//bool(false) When there is no element, the value returned
//each() cooperates with while traversal
$user=array("id" =>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while($arr=each($user))
{
//echo $arr[0]."====>".$arr[1]."
";//Display the key (subscript) and Value
echo $arr["key"]."===>".$arr["value"]."
";//Display key value through key, value
}
//Usage of list() function
list($name,$age,$sex)=array("zhangsan",10,"nnnnn");
echo $name."
";
echo $age."
";
echo $sex."
";
//Another way to use it
list(, ,$sex)=array("zhangsan",10,"nnnnn");
echo $sex."
";//Only convert gender into variables
//ip judgment
$ip="192.168.1.128";
list(,,,$d)=explode(".",$ip);//explode means separated by . and returns an array
echo $d ;//Get out 128
//List() can only receive examples of index arrays
$user=array("id"=>1,"name"=>"zhangsan","age"= >10,"sex"=>"nan");
list($key,$value)=each($user);//Array( [1]=>1 [0]=> id) assign values to the parameters in the list in order of index subscript, so first the 0 key and then the 1 value
echo $key."--->".$value;
//while list() each () Use in combination
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."
";
}
//Solution to display only once in multiple loops
//Use the internal pointer control function of the array
//next(array); the array pointer moves to the next
//prev(array); the array pointer moves to the previous one
//reset(array); the array pointer moves to the first one (reset)
//end(array); the array pointer moves to the last one
//current(array); Get the value of the current element, which is the element pointed to by the array pointer.
//key(array); Get the key value (subscript) of the current element
$user=array("id"=>1,"name"=>"zhangsan","age"= >10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--- >".$value."
";
}
//Move the array pointer here to the first one and the following loop will output
//reset($user)
while(list($key,$value)=each($user))//Because each() returns false to the last one, the loop jumps out directly
{
echo $key."---> ;".$value."
";
}
while(list($key,$value)=each($user))//Because each() returns false to the last one, so The loop jumps out directly
{
echo $key."--->".$value."
";
}
echo current($user)."=== ==>".key($user);
?>