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:
Foreach
'good', 'swimming' => 'very well', 'running' => 'not good');foreach ($sports as $key => $value) { echo $key.": ".$value."
";}?>
Program running result:
football: goodswimming: very wellrunning: not good
Each
'good', 'swimming' => 'very well', 'running' => 'not good');while ($elem = each($sports)) { echo $elem['key'].": ".$elem['value']."
";}?>
Program running result:
football: goodswimming: very wellrunning: not good
List & each
'good', 'swimming' => 'very well', 'running' => 'not good');while (list($key, $value) = each($sports)) { echo $key.": ".$value."
";}?>
Program running result:
football: goodswimming: very wellrunning: not good