PHP array traversal method (foreach, list, each ). In PHP, arrays are divided into two types: numerical index arrays and associated arrays. The numeric index array is the same as the array in C language. the subscript is 0, 1, 2... The joined array subscript may be any array in PHP divided into two categories: the number index array and the joined array.
The numeric index array is the same as the array in C language. the subscript is 0, 1, 2...
The subscript of the correlated array may be of any type, which is similar to the hash and map structures in other languages.
The following describes three methods to traverse joined arrays in PHP:
Method 1: foreach
The code is as follows:
$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good ');
Foreach ($ sports as $ key => $ value ){
Echo $ key. ":". $ value ."
";
?>
Output result:
Football: good
Faster Ming: very well
Running: not good
Method 2: each
The code is as follows:
$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good ');
While ($ elem = each ($ sports )){
Echo $ elem ['key']. ":". $ elem ['value']."
";
?>
Method 3: list & each
The code is as follows:
$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good ');
While (list ($ key, $ value) = each ($ sports )){
Echo $ key. ":". $ value ."
";
?>
Numeric index array and associated array. The numeric index array is the same as the array in C language. the subscript is 0, 1, 2... The subscript of the associated array may be arbitrary...