Maison > développement back-end > tutoriel php > PHP 遍历数组语句总结(foreach,for,list,each)_PHP教程

PHP 遍历数组语句总结(foreach,for,list,each)_PHP教程

WBOY
Libérer: 2016-07-21 14:53:18
original
971 Les gens l'ont consulté

 foreach来访问, 遍历的顺序是固定的么? 以什么顺序遍历呢?

比如:

代码如下

$colors= array('red','blue','green','yellow');
foreach ($colors as $color){
//add your codes
}
?>

例2

$capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
foreach($capitals as $key=> $val){
//add your codes
}

 


while()

while() 通常和 list(),each()配合使用。

#example2:

代码如下

$colors = array('red','blue','green','yellow');

while(list($key,$val) = each($colors)) {
echo "Other list of $val.
";
}
?>

显示结果:

Other list of red.
Other list of blue.
Other list of green.
Other list of yellow.

 

3. for()
#example3:

代码如下


$arr = array ("0" => "zero","1" => "one","2" => "two");

for ($i = 0;$i $str = $arr[$i];
echo "the number is $str.
";
}
?>
显示结果:

the number is zero.
the number is one.
the number is two.

 

========= 以下是函数介绍 ==========

key()
mixed key(array input_array)

key()函数返回input_array中位于当前指针位置的键元素。

#example4

代码如下


$capitals = array("Ohio" => "Columbus","Towa" => "Des Moines","Arizona" => "Phoenix");
echo "

Can you name the capitals of these states?

";
while($key = key($capitals)) {
echo $key."
";
next($capitals);
//每个key()调用不会推进指针。为此要使用next()函数
}
?>

Can you name the capitals of these states?
Ohio
Towa
Arizona

 

each() 函数遍历数组

例子 1

代码如下

$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>

输出:

Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )

 

子 2
each() 经常和 list() 结合使用来遍历数组。本例与上例类似,不过循环输出了整个数组:

代码如下

复制代码

$people = array("Peter", "Joe", "Glenn", "Cleveland");

reset($people);

while (list($key, $val) = each($people))
{
echo "$key => $val
";
}
?>

 

输出:

0 => Peter
1 => Joe
2 => Glenn
3 => Cleveland


多维数组的递归遍历

代码如下

/*
* -------------------------------------------------
* Author :
* Url : www.45it.com* Date : 2011-03-09
* -------------------------------------------------
*/
function arr_foreach ($arr)
{
if (!is_array ($arr))
{
return false;
}

foreach ($arr as $key => $val )
{
if (is_array ($val))
{
arr_foreach ($val);
}
else
{
echo $val.'
';
}
}
}

$arr1 = array (1=>array(11,12,13,14=>array(141,142)),2,3,4,5);

echo '

';<br>
print_r($arr1);<br>
echo '<pre class="brush:php;toolbar:false">';
<p>arr_foreach ($arr1);<br>
?></p>
<p>结果</p>
<p>Array<br>
(<br>
[1] => Array<br>
(<br>
[0] => 11<br>
[1] => 12<br>
[2] => 13<br>
[14] => Array<br>
(<br>
[0] => 141<br>
[1] => 142<br>
)</p>
<p>)</p>
<p>[2] => 2<br>
[3] => 3<br>
[4] => 4<br>
[5] => 5<br>
)<br>
11<br>
12<br>
13<br>
141<br>
142<br>
2<br>
3<br>
4<br>
5</p> 
<p align="left"></p><div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/371384.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/371384.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">foreach来访问, 遍历的顺序是固定的么? 以什么顺序遍历呢? 比如: 代码如下 ?php $colors= array('red','blue','green','yellow'); foreach ($colors as $color){ //...</span>
</div>
<div class="art_confoot"></div>
Copier après la connexion
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal