如何使用foreach来操作数组?foreach操作数组实例详解

伊谢尔伦
Lepaskan: 2023-03-11 09:06:01
asal
1327 orang telah melayarinya

foreach()有两种用法: 

foreach(array_name as $value) 
{ 
statement; 
}
Salin selepas log masuk

这里的array_name是你要遍历的数组名,每次循环中,array_name数组的当前元素的值被赋给$value,并且数组内部的下标向下移一 步,也就是下次循环回得到下一个元素。

foreach(array_name as $key => $value) 
{ 
statement; 
}
Salin selepas log masuk

这里跟第一种方法的区别就是多了个$key,也就是除了把当前元素的值赋给$value外,当前元素的键值也会在每次循环中被赋给变量$key。键值可以 是下标值,也可以是字符串。比如book[0]=1中的"0",book[id]="001"中的"id".
程序实例1:

<?php 
/*-------------------------------------------------------------------------*/ 
/* foreach example 1: value only */ 
echo "foreach example 1: value only ".&#39;<br />&#39;; 
$a = array(1, 2, 3, 17); 
foreach ($a as $v) { 
echo "
Current
 value of ".$a.":". $v."<br />"; 
} 
?>
Salin selepas log masuk

// 运行结果
foreach example 1: value only
Current value of $a: 1
Current value of $a: 2
Current value of $a: 3
Current value of $a: 17

2 代码如下:

/*-------------------------------------------------------------------------*/ 
/* foreach example 2: value (with key printed for illustration) */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 2: value (with key printed for illustration) ".&#39;<br />&#39;; 
$a = array(1, 2, 3, 17); 
$i = 0; /* for illustrative purposes only */ 
foreach ($a as $v) { 
echo ""$a[$i] => $v".&#39;<br />&#39;; 
$i++; 
} 
// 程序运行结果 
foreach example 2: value (with key printed for illustration) 
$a[0] => 1 
$a[1] => 2 
$a[2] => 3 
$a[3] => 17
Salin selepas log masuk

3 代码如下:

/*-------------------------------------------------------------------------*/ 
/* foreach example 3: key and value */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 3: key and value ".&#39;<br />&#39;; 
$a = array( 
"one" => 1, 
"two" => 2, 
"three" => 3, 
"seventeen" => 17 
); 
foreach ($a as $k => $v) { 
echo ""$a[$k] => $v".&#39;<br />&#39;; 
} 
// 程序运行结果 
foreach example 3: key and value 
$a[one] => 1 
$a[two] => 2 
$a[three] => 3 
$a[seventeen] => 17
Salin selepas log masuk

4 代码如下:

/*-------------------------------------------------------------------------*/ 
/* foreach example 4: multi-dimensional arrays */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 4: multi-dimensional arrays ".&#39;<br />&#39;; 
$a = array(); 
$a[0][0] = "a"; 
$a[0][1] = "b"; 
$a[1][0] = "y"; 
$a[1][1] = "z"; 
foreach ($a as $v1) { 
foreach ($v1 as $v2) { 
echo "$v2"n"; 
} 
} 
// 程序运行结果 
foreach example 4: multi-dimensional arrays 
a b y z
Salin selepas log masuk

5 代码如下:

/*-------------------------------------------------------------------------*/ 
/* foreach example 5: dynamic arrays */ 
echo &#39;<br />&#39;.&#39;<br />&#39;."foreach example 5: dynamic arrays ".&#39;<br />&#39;; 
foreach (array(1, 2, 3, 4, 5) as $v) { 
echo "$v"n"; 
} 
// 程序运行结果 
foreach example 5: dynamic arrays 
1 2 3 4 5
Salin selepas log masuk

也可以这么用:

$messageNav[&#39;首页&#39;] =ROOT_PATH; 
$messageNav[&#39;人才交流&#39;] ="#" 
$messageNav[&#39;动态专栏&#39;] ="hragent/cn/" 
<?php $i = 0;foreach ($messageNav as $key=>$value):?> 
<?php if ($i != count($messageNav) - 1):?> 
<a href="<?=$value?>"><?=$key?></a>> 
<?php else:?> 
<a href="<?=$value?>" class="onlink"><?=$key?></a> 
<?php endif;?> 
<?php $i++;endforeach;?>
Salin selepas log masuk


Atas ialah kandungan terperinci 如何使用foreach来操作数组?foreach操作数组实例详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan