php中用foreach來運算元組的代碼

來源:互聯網
上載者:User

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 ".'<br />';
$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 '<br />'.'<br />'."foreach example 2: value (with key printed for illustration) ".'<br />';
$a = array(1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
echo ""$a[$i] => $v".'<br />';
$i++;
}
// 程式運行結果
foreach example 2: value (with key printed for illustration)
$a[0] => 1
$a[1] => 2
$a[2] => 3
$a[3] => 17

3 複製代碼 代碼如下:/*-------------------------------------------------------------------------*/
/* foreach example 3: key and value */
echo '<br />'.'<br />'."foreach example 3: key and value ".'<br />';
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo ""$a[$k] => $v".'<br />';
}
// 程式運行結果
foreach example 3: key and value
$a[one] => 1
$a[two] => 2
$a[three] => 3
$a[seventeen] => 17

4 複製代碼 代碼如下:/*-------------------------------------------------------------------------*/
/* foreach example 4: multi-dimensional arrays */
echo '<br />'.'<br />'."foreach example 4: multi-dimensional arrays ".'<br />';
$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

5 複製代碼 代碼如下:/*-------------------------------------------------------------------------*/
/* foreach example 5: dynamic arrays */
echo '<br />'.'<br />'."foreach example 5: dynamic arrays ".'<br />';
foreach (array(1, 2, 3, 4, 5) as $v) {
echo "$v"n";
}
// 程式運行結果
foreach example 5: dynamic arrays
1 2 3 4 5

也可以這麼用: 複製代碼 代碼如下:$messageNav['首頁'] =ROOT_PATH;
$messageNav['人才交流'] ="#"
$messageNav['動態專欄'] ="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;?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.