The most flexible things in php are arrays, and a lot of data is displayed in arrays. here we will sort out the array traversal method, which can be selected as needed. 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 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 ."
";
?>