<?php
/* Traversal of an array
*
* 1. use for The statement loop iterates over groups
* 1. Other languages ( Only this 1 Kind of way )
* 2.PHP This is not the preferred method
* 3. An array must be an indexed array, and the subscripts must be continuous.
* ( Index subscripts can be discontinuous , Arrays also have associative arrays , These two can't be traversed )
*
* 2. use foreach The statement loop iterates over groups
* foreacho( An array variable as A variable's value ){
* // The loop body
* }
* 1. The number of loops is determined by the number of elements in the array
* 2. every 1 Each loop assigns the elements of the array to the following variables
*
* foreach( An array variable as The subscript variables => Values of a variable ){
* }
*
*
* 3.while() list() each() The combination loop traverses the groups
*
* each() Function:
* 1. Need to be 1 Two arrays as parameters
* 2. And so does the return 1 An array
* 3. The returned array is 0,1,key,value4 A subscript ( fixed )
* 0 and key The subscript is the key of the element of the current parameter array
* 1 and value The index is the value of the element in the current parameter array
* 4. The default current element is the first 1 An element
* 5. Each time 1 This will move the current element backwards
* 6. Returns if the last element executes the function false
* list() Function:
* 1. list()=array(); You need to 1 Two arrays are assigned to this function
* 2. The number of elements in the array list() The function has the same number of arguments
* 3. Each element in the array is assigned a value list() Each parameter in the function ,list() Turn each parameter into a variable
* 4.list() Only indexed arrays can be accepted
* 5. Assign parameters in index order
*
*
*
*/
//for Statement iterates over groups
$user=array(1,"zhangsan",40,"nan");
for($i=0;$i<4;$i++)
{
echo"\$user[{$i}]=".$user[$i]."<br>";
}
// use foreach
$user=array(1,"zhangsan",40,"nan");
foreach($user as $val)//$val Is a custom variable
{
echo $val."<br>";// The output is independent of the index
}
foreach($user as $key=>$val)//$val $key All are custom variables
{
echo $key."=====>".$val."<br>";
}
//foreach Traversing the multidimensional array
$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 '<table align="center" width="500" border="1">';
echo '<caption><h1>'.$tableName.'</h1></caption>';
foreach($table as $row)
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>'.$col.'</td>';
}
echo '</tr>';
}
echo '</table>';
}
//each() The use of
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
$a=each($user);//Array ( [1] => 1 [value] => 1 [0] => id [key] => id ) The default is the first 1 The value of
print_r($a);
$b=each($user);
print_r($b);//Array ( [1] => zhangsan [value] => zhangsan [0] => name [key] => name ) Each time 1 Times, traversing backwards 1 a
$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) The value returned when there is no element
//each() Cooperate with while traverse
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while($arr=each($user))
{
//echo $arr[0]."====>".$arr[1]."<br>";// through 0,1 To display the key ( The subscript ) and value
echo $arr["key"]."===>".$arr["value"]."<br>";// through key,value To display the key value
}
//list() Use of functions
list($name,$age,$sex)=array("zhangsan",10,"nnnnn");
echo $name."<br>";
echo $age."<br>";
echo $sex."<br>";
// On the other 1 Method of use
list(,,$sex)=array("zhangsan",10,"nnnnn");
echo $sex."<br>";// Just convert gender to variables
//ip judge
$ip="192.168.1.128";
list(,,,$d)=explode(".",$ip);//explode Said with . To separate and return 1 An array
echo $d;// Take out the 128
//list() Only examples of indexed arrays can be received
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
list($key,$value)=each($user);//Array( [1]=>1 [0]=>id) In the order of index index list , so first 0 key And then the 1 value
echo $key."--->".$value;
//while list() each() Use a combination of
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."<br>";
}
// Multiple loops only show 1 Second solution
// USES the internal pointer control function of the array
//next( An array of ); The array pointer moves down 1 a
//prev( An array of ); The array pointer is moved up 1 a
//reset( An array of ); The array pointer moves to first 1 a ( reset )
//end( An array of ); The array pointer moves to the end 1 a
//current( An array of ); Gets the value of the current element , The element to which the index group pointer points at the current element.
//key( An array of ); Gets the key value of the current element ( The subscript )
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."<br>";
}
// Here move the array pointer to first 1 The output can be done in the following loops
//reset($user)
while(list($key,$value)=each($user))// because each() In the end 1 A return false So the loop just jumps out
{
echo $key."--->".$value."<br>";
}
while(list($key,$value)=each($user))// because each() In the end 1 A return false So the loop just jumps out
{
echo $key."--->".$value."<br>";
}
echo current($user)."=====>".key($user);
?>