首頁 >後端開發 >php教程 >如何使用foreach來操作數組? foreach操作數組實例詳解

如何使用foreach來操作數組? foreach操作數組實例詳解

伊谢尔伦
伊谢尔伦原創
2017-06-23 15:00:151338瀏覽

foreach()有兩種用法: 

foreach(array_name as $value) 
{ 
statement; 
}

這裡的array_name是你要遍歷的陣列名,每次#在##循環中,array_name陣列的目前元素的值被賦給$value,並且陣列內部的下標向下移一步,也就是下次迴圈回得到下一個元素。

foreach(array_name as $key => $value) 
{ 
statement; 
}

這裡跟第一種方法的差別就是多了個$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 />"; 
} 
?>

// 執行結果

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

 程式碼如下:

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

程式碼如下:

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

程式碼如下:

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

也可以這麼用: 

$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;?>


#

以上是如何使用foreach來操作數組? foreach操作數組實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn