php method sharing for traversing groups

  • 2020-05-16 06:29:03
  • OfStack

There are two types of arrays in PHP: numeric indexed arrays and associative arrays.
Where the number index array and C language array 1, subscript is 0,1,2...
The index of associative array may be of any type, similar to the structure of hash, map and so on in other languages.
Method 1: foreach
 
<?php 
$sports = array( 
'football' => 'good', 
'swimming' => 'very well', 
'running' => 'not good'); 
foreach ($sports as $key => $value) { 
echo $key.": ".$value."<br />"; 
} 
?> 

Output results:
football: good
swimming: very well
running: not good
Method 2: each
 
<?php 
$sports = array( 
'football' => 'good', 
'swimming' => 'very well', 
'running' => 'not good'); 
while (!!$elem = each($sports)) { 
echo $elem['key'].": ".$elem['value']."<br />"; 
} 
?> 

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

Method 3: list & each
 
<?php 
$sports = array( 
'football' => 'good', 
'swimming' => 'very well', 
'running' => 'not good'); 
while (!!list($key, $value) = each($sports)) { 
echo $key.": ".$value."<br />"; 
} 
?> 

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

Related articles: