欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP 數(shù)組遍歷方法大全(foreach,list,each)

 更新時(shí)間:2010年06月30日 16:46:15   作者:  
php下最靈活的東西都是數(shù)組,很多數(shù)據(jù)都是通過(guò)數(shù)組的方式顯示,這里整理下數(shù)組的遍歷方法,大家可以根據(jù)需要選用。
在PHP中數(shù)組分為兩類: 數(shù)字索引數(shù)組和關(guān)聯(lián)數(shù)組。
其中數(shù)字索引數(shù)組和C語(yǔ)言中的數(shù)組一樣,下標(biāo)是為0,1,2…
而關(guān)聯(lián)數(shù)組下標(biāo)可能是任意類型,與其它語(yǔ)言中的hash,map等結(jié)構(gòu)相似。

下面介紹PHP中遍歷關(guān)聯(lián)數(shù)組的三種方法:

方法1:foreach

復(fù)制代碼 代碼如下:

<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
foreach ($sports as $key => $value) {
echo $key.": ".$value."<br />";
?>

輸出結(jié)果:

football: good
swimming: very well
running: not good

方法2:each

復(fù)制代碼 代碼如下:

<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while ($elem = each($sports)) {
echo $elem['key'].": ".$elem['value']."<br />";
?>


方法3:list & each
復(fù)制代碼 代碼如下:

<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good');
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."<br />";
?>

相關(guān)文章

最新評(píng)論