數組的指標有關問題

來源:互聯網
上載者:User
數組的指標問題


$arr = array(
array(
'id'=>1,
'title'=>'測試1',
),
array(
'id'=>2,
'title'=>'測試2',
),
array(
'id'=>3,
'title'=>'測試3',
),
array(
'id'=>4,
'title'=>'測試4',
),
array(
'id'=>5,
'title'=>'測試5',
),
);

while(list($k, $v) = each($arr)){

print_r(current($arr));

echo '
';

}



迴圈一個二維數組,使用 current 返回當前元素,為啥從第2個元素開始的呢? 想要從1開始怎麼做,請前輩指教

分享到:


------解決方案--------------------
第一次執行到while(list($k, $v) = each($arr))這裡時(注:each -- 返回數組中當前的鍵/值對並將數組指標向前移動一步 )
也就是說 each($arr)已經令$arr數組的指標下移一位
所以是從第2個元素開始.
要從第一個開始就這樣吧:
do
{
print_r(current($arr));

echo '
';
}
while (list($k, $v) = each($arr));

希望能幫到你.
------解決方案--------------------
current函數是返回當前數組單元。。。你第一次while判斷時each已經將數組單元指向下一位了。。結果就從2開始杯具了
------解決方案--------------------
改用do就可以了,因為while是指標先前移動一位了
$arr = array(
array(
'id'=>1,
'title'=>'測試1',
),
array(
'id'=>2,
'title'=>'測試2',
),
array(
'id'=>3,
'title'=>'測試3',
),
array(
'id'=>4,
'title'=>'測試4',
),
array(
'id'=>5,
'title'=>'測試5',
),
);
do{

print_r(current($arr));

echo '
';
}

while(list($k, $v) = each($arr));



?>


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.