|
其中數字索引數組和C語言中的數組一樣,下標是為0,1,2…
而關聯數組下標可能是任意類型,與其它語言中的hash,map等結構相似。
下面介紹php中遍歷關聯數組的三種方法:
方法1:foreach
復制代碼 代碼如下:
<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
foreach ($sports as $key => $value) {
echo $key.": ".$value."<br />";
?>
輸出結果:
football: good
swimming: very well
running: not good
方法2:each
復制代碼 代碼如下:
<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while ($elem = each($sports)) {
echo $elem['key'].": ".$elem['value']."<br />";
?>
方法3:list & each
復制代碼 代碼如下:
<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."<br />";
?>
php技術:PHP 數組遍歷方法大全(foreach,list,each),轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。