Home > Backend Development > PHP Tutorial > Sharing how to traverse arrays in php_PHP tutorial

Sharing how to traverse arrays in php_PHP tutorial

WBOY
Release: 2016-07-21 15:19:47
Original
771 people have browsed it

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.
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']."
";
}
?>

Output result:
football: good
swimming : very well
running: not good

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."
";
}
?>

Output result:
football: good
swimming: very well
running: not good

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325226.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template