Home  >  Article  >  Backend Development  >  PHP array loop output implementation method_PHP tutorial

PHP array loop output implementation method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:381586browse

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
 代码如下 复制代码

'bacon and eggs', 'lunch' => 'roast beef', 'dinner' => 'lasagna');

/* 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 */

$result = array_values($menu); print_r($result);

?>

'bacon and eggs', 'lunch' => 'roast beef', 'dinner' => 'lasagna');

/* 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 */

 代码如下 复制代码
list = array('upid'=>'1','title'=>'公司新闻','list'=>array('id'=>'3','title'=>'公司新闻标题测试!','intime'=>'2009-29-5'));
foreach ($list as $v=>$a)
{
  echo $a['upid'] .'-'. $a['title'];
  foreach ($a['list'] as $b){
    echo $b['title'];
  }
 
}
$result = array_values($menu); print_r($result);

?>

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

}

Use array keys
 代码如下 复制代码

<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,'
'; } ?>
Output of running the example: wang: 18 li: 20 zhang: 25 When foreach starts executing, the pointer inside the array will automatically point to the first element, which means there is no need to call reset() before the foreach loop. for() loops through the array If you are operating an array of continuous key values, you can also use a for() loop to traverse the array:
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


*/

while(list($key,$val) = each($colors)) {
The code is as follows
 代码如下 复制代码

$colors = array('red','blue','green','yellow');
 
while(list($key,$val) = each($colors)) {
    echo "other list of $val.
";
}

Copy code


$colors = array('red','blue','green','yellow');
echo "other list of $val.
"; }

For more details, please see: http://www.bKjia.c0m/phper/php/36112.htm http://www.bkjia.com/PHPjc/629144.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/629144.htmlTechArticleIn the past, we often thought about how to implement php array loop output. This article introduces the use of commonly used methods in php Four loop statements are used to implement single array or multi-dimensional array loop output methods. In...
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