in PHP, arrays are divided into two categories: numbersIndex arrays and associative arrays.
Where the numeric index array is the same as the array in the C language, the subscript is for 0,1,2 ...
Associative array subscripts may be of any type, similar to Hash,map in other languages.
Here are a few ways to iterate through associative arrays in PHP:
Method 1:foreach
foreach () is the simplest and most efficient way to iterate through the data in an array.
<?php $sports = Array (' Football ' = ' good ', ' swimming ' = ' very well ', ' running ' + ' not good '); foreach ($sports as $key = + $value) {echo $key. ":". $value. " <br/> ";?>
Output Result:
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/> ";?>
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/> ";?>
Method 4:while () and list () are used with each ().
<?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); while (list ($key, $val) = each ($urls)) { echo ' This Site URL is $val. <br/> "; }? >
Show Results:
This site URL is aaathis site URL was bbbthis site URL is cccthis site URL is ddd
Method 5:for ()
<?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); for ($i = 0; $i < count ($urls), $i + +) { $str = $urls [$i]; echo "This Site URL is $str. <br/>"; }? >
Show Results:
This site URL is aaathis site URL was bbbthis site URL is cccthis site URL is ddd