在PHP中數組分為兩類: 數字索引數組和關聯陣列。其中數字索引數組和C語言中的數組一樣,下標是為0,1,2…而關聯陣列下標可能是任意類型,與其它語言中的hash,map等結構相似。
下面介紹PHP中遍曆關聯陣列的三種方法:
foreach
'good', 'swimming' => 'very well', 'running' => 'not good');foreach ($sports as $key => $value) { echo $key.": ".$value."
";}?>
程式運行結果:
football: goodswimming: very wellrunning: not good
each
'good', 'swimming' => 'very well', 'running' => 'not good');while ($elem = each($sports)) { echo $elem['key'].": ".$elem['value']."
";}?>
程式運行結果:程式運行結果:
football: goodswimming: very wellrunning: not good
list & each
'good', 'swimming' => 'very well', 'running' => 'not good');while (list($key, $value) = each($sports)) { echo $key.": ".$value."
";}?>
程式運行結果:
football: goodswimming: very wellrunning: not good
http://www.bkjia.com/PHPjc/755774.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/755774.htmlTechArticle在PHP中數組分為兩類: 數字索引數組和關聯陣列。其中數字索引數組和C語言中的數組一樣,下標是為0,1,2…而關聯陣列下標可能是任意...