Copy codeThe Code is as follows:
<? Php
/* Array Traversal
*
* 1. Use the for statement to traverse the array cyclically
* 1. Other languages (only in this way)
* 2. This method is not the preferred method in PHP.
* 3. The array must be an index array and the subscript must be continuous.
* (Index array subscripts can be discontinuous, and arrays can also be associated arrays. These two types cannot be traversed)
*
* 2. Use the foreach statement to traverse the array cyclically
* 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 assigns the elements in the array to the following variables respectively.
*
* Foreach (array variable as subscript variable => value variable ){
*}
*
*
* 3. while () list () each () combined loop traversal Array
*
* Each () function:
* 1. An array is required as a parameter.
* 2. The returned result is an array.
* 3. The returned array is 0, 1, key, and value subscripts (fixed)
* 0 and key subscript are the keys of the elements in the current parameter array.
* 1 and value subscript are the values of the elements in the current parameter array.
* 4. By default, the current element is the first element.
* 5. After each execution, the current element is moved backward.
* 6. If the last element executes this function, false is returned.
* List () function:
* 1. list () = array (); assign an array to this function.
* 2. The number of elements in the array must be the same as the number of parameters in the list () function.
* 3. Each element value in the array is assigned a value to each parameter in the list () function. list () converts each parameter to a variable.
* 4. list () can only accept indexed Arrays
* 5. assign values to parameters in the subscript order of indexes.
*
*
*
*/
// For statement to traverse the Array
$ 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 has nothing to do with the subscript.
}
Foreach ($ user as $ key => $ val) // $ val $ key is a custom variable
{
Echo $ key. "=====>". $ val. "<br> ";
}
// 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, '123', 'aaa @ bbb.com '),
Array (2, '123', 'bbb @ ccc.com '),
Array (3, '123', 'ccc @ ddd.com ')
)
);
Foreach ($ info as $ tableName => $ table)
{
Echo '<table align = "center" width = "500" border = "1"> ';
Echo '<caption> Foreach ($ table as $ row)
{
Echo '<tr> ';
Foreach ($ row as $ col)
{
Echo '<td>'. $ col. '</td> ';
}
Echo '</tr> ';
}
Echo '</table> ';
}
// Use of each ()
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
$ A = each ($ user); // Array ([1] => 1 [value] => 1 [0] => id [key] => id) the default value is the value of the first element.
Print_r ($ );
$ B = each ($ user );
Print_r ($ B); // Array ([1] => zhangsan [value] => zhangsan [0] => name [key] => name) each time, traverse 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) value returned when no element exists
// Each () and while Traversal
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While ($ arr = each ($ user ))
{
// Echo $ arr [0]. "===> ". $ arr [1]. "<br>"; // displays the key (subscript) and value through 0, 1.
Echo $ arr ["key"]. "==>". $ arr ["value"]. "<br>"; // key and value are used to display key values.
}
// Use the list () function
List ($ name, $ age, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ name. "<br> ";
Echo $ age. "<br> ";
Echo $ sex. "<br> ";
// ANOTHER METHOD
List (, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ sex. "<br>"; // only convert gender to variable
// Ip judgment
$ Ip = "192.168.1.128 ";
List (, $ d) = explode (".", $ ip); // explode is separated by. And an array is returned.
Echo $ d; // retrieve 128
// List () can only receive indexed 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 according to the order of the index, so the values are 0 and 1.
Echo $ key. "--->". $ value;
// While list () each () Combination
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While (list ($ key, $ value) = each ($ user ))
{
Echo $ key. "--->". $ value. "<br> ";
}
// The solution is to display only once for multiple cycles
// Use the internal pointer control function of the array
// Next (array); move the array pointer to the next
// Prev (array); move the array pointer to the previous one
// Reset (array); move the array pointer to the first one (reset)
// End (array); the array pointer moves to the last one
// Current (array); obtains the value of the current element. The element pointed to by the index group pointer when the current element is present.
// Key (array); obtains 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. "<br> ";
}
// Here, move the array pointer to the first loop below to output
// Reset ($ user)
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
{
Echo $ key. "--->". $ value. "<br> ";
}
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
{
Echo $ key. "--->". $ value. "<br> ";
}
Echo current ($ user). "====>". key ($ user );
?>