Home>Article>Backend Development> The efficacy and function of Hedyotis diffusa and how to consume it. Sharing the method of traversing arrays in PHP

The efficacy and function of Hedyotis diffusa and how to consume it. Sharing the method of traversing arrays in PHP

WBOY
WBOY Original
2016-07-29 08:48:16 1276browse

Arrays are divided into two categories in PHP: numerically indexed arrays and associative arrays.
The numerical 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 the codeThe 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 the codeThe 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 codeThe 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

The above has introduced the efficacy, function and consumption method of Hedyotis diffusa. The method of traversing the array in PHP is shared, including the efficacy, function and consumption method of Hedyotis diffusa. I hope it will be helpful to friends who are interested in PHP tutorials. .

Statement:
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