Arrays are divided into two categories in PHP: numerically indexed arrays and associative arrays.
The numeric index array is the same as the array in C language, the subscripts are 0, 1, 2...
The subscript of the associative array may be of any type, similar to hash, map and other structures in other languages.
The following introduces three methods of traversing associative arrays in PHP:
Method 1: foreach
Copy Code The code is as follows:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
foreach ($sports as $key => $value) {
echo $key.": " .$value."
";
?>
Output result:
football: good
swimming: very well
running: not good
Method 2: each
Copy code The code is as follows:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while ($elem = each($sports)) {
echo $elem['key'].": ".$elem['value']."< ;br />";
?>
Method 3: list & each
Copy Code The code is as follows:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while (list($key, $value) = each($sports)) {
echo $key .": ".$value."
";
?>
http://www.bkjia.com/PHPjc/322119.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322119.htmlTechArticleArrays in PHP are divided into two categories: numeric index arrays and associative arrays. The numeric index array is the same as the array in C language. The subscripts are 0, 1, 2... and the associative array subscripts may be arbitrary...