Home >Backend Development >PHP Tutorial >PHP array loop output implementation method_PHP tutorial
In the past, we often thought about how to achieve php array loop output. This article introduces the use of four commonly used loop statements in php to achieve single array or multi-dimensional array loop output.
In PHP, we can use the following loop statements:
while
As long as the specified condition is true, the code block will be executed in a loop
do...while
First execute the block of code once, then repeat the loop when the specified condition is true
for
Loop through the code block a specified number of times
foreach
Loop a block of code based on each element in the array
First we use PHP’s built-in functions to traverse the PHP array
The array_keys() and array_values() functions are readily available to get a list of all keys and corresponding values in the array.
The code is as follows | Copy code | ||||||||
/* returns the array ('breakfast', 'lunch', 'dinner') with numeric indices */ $result = array_keys($menu); print_r($result); print " ";/* returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */
?> |
Use foreach to traverse php array
代码如下 | 复制代码 |
$arr_age = array("wang"=>18, "li"=>20, "zhang"=>25); foreach ($arr_age as $key=>$age) { echo $key,': ',$age,' '; } ?> |
PHP foreach() syntax structure is used to traverse operations or output arrays. foreach() can only be used to traverse arrays or objects. An error will occur when trying to use it for other data types or an uninitialized variable.
The code is as follows | Copy code |
list = array('upid'=> '1','title'=>'Company News','list'=>array('id'=>'3','title'=>'Company News Title Test!','intime' =>'2009-29-5')); foreach ($list as $v=>$a) { echo $a['upid'] .'-'. $a['title']; foreach ($a['list'] as $b){echo $b['title']; } } |
代码如下 | 复制代码 |
<br> <?php<br /> $shuzu=array("ni","wo","ta","php","mysql");<br /> $count=count($shuzu);<br /> echo "使用for遍历数组";<br /> echo "<br/>$nbsp;<br/>";<br> for($i=0;$i<$count;$i++)<br> {<br> $j=$i+1;<br> echo "第{$j}个元素是: $shuzu[$i]";<br> echo "<br/>$nbsp;<br/>";<br> }<br> ?><br> 25 |
The code is as follows | Copy code |
$arr_age = array("wang"=>18, "li"=>20, "zhang"=>25);
foreach ($arr_age as $key=>$age) {
echo $key,': ',$age,' '; } ?> |
The code is as follows | Copy code |
<?php $shuzu=array("ni","wo","ta","php","mysql"); $count=count($shuzu); echo "Use for to traverse the array"; echo "<br/>$nbsp;<br/>"; for($i=0;$i<$count;$i++) { $j=$i+1; echo "The {$j}th element is: $shuzu[$i]"; echo "<br/>$nbsp;<br/>"; } ?>25 |
You can also use list() and each() combined to traverse the php array, but testing found that it is not as efficient as foreach().
list function
The list() function assigns values to a set of variables using elements in the array.
Note that, like array(), list() is actually a language construct, not a function.
Grammar
list(var1,var2...) parameter description
var1 required. The first variable to be assigned a value.
var2 Optional. There can be multiple variables.
Tips and Notes
Note: This function only works with numerically indexed arrays, and assumes that numerical indexing starts from 0
*/
The code is as follows
|
Copy code
|
||||
$colors = array('red','blue','green','yellow'); |
www.bkjia.com