php关联数组和哈希表,php遍历哈希表及关联数组的实例代码
有關php數(shù)組的分類,PHP數(shù)組分為:數(shù)字索引數(shù)組和關聯(lián)數(shù)組。
其中數(shù)字索引數(shù)組和C語言中的數(shù)組一樣,下標是為0,1,2…
而關聯(lián)數(shù)組下標可能是任意類型,與其它語言中的hash,map等結構相似。
PHP遍歷關聯(lián)數(shù)組的三種方法,供大家參考。
方法1:foreach
復制代碼 代碼示例:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running'? => 'not good');
foreach ($sports as $key => $value) {
echo $key.": ".$value."
";
} //by www.jbxue.com
?>
輸出結果:
football: goodswimming: very wellrunning: not good
方法2:each
復制代碼 代碼示例:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running'? => 'not good');
while ($elem = each($sports)) {
echo $elem['key'].": ".$elem['value']."
";
}
?>
方法3:list & each
復制代碼 代碼示例:
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running'? => 'not good');
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."
";
}
?>
學php教程,就來腳本學堂,祝大家學習進步。
總結
以上是生活随笔為你收集整理的php关联数组和哈希表,php遍历哈希表及关联数组的实例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北京环球影城霸天虎过山车身高限制
- 下一篇: td 双击 编辑 php,双击表格td进