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
<?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
<?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 />";
?>
其中數(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 />";
?>
您可能感興趣的文章:
- PHP遍歷數(shù)組的方法匯總
- PHP遍歷數(shù)組的幾種方法
- PHP循環(huán)遍歷數(shù)組的3種方法list()、each()和while總結(jié)
- 探討php中遍歷二維數(shù)組的幾種方法詳解
- PHP中多維數(shù)組的foreach遍歷示例
- PHP 數(shù)組遍歷foreach語(yǔ)法結(jié)構(gòu)及實(shí)例
- php實(shí)現(xiàn)遍歷多維數(shù)組的方法
- PHP中使用foreach()遍歷二維數(shù)組的簡(jiǎn)單實(shí)例
- PHP foreach遍歷多維數(shù)組實(shí)現(xiàn)方式
- PHP遍歷數(shù)組的6種方式總結(jié)
相關(guān)文章
基于wordpress主題制作的具體實(shí)現(xiàn)步驟
本篇文章是對(duì)wordpress主題制作的具體實(shí)現(xiàn)步驟進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05php實(shí)現(xiàn)登錄tplink WR882N獲取IP和重啟的方法
這篇文章主要介紹了php實(shí)現(xiàn)登錄tplink WR882N獲取IP和重啟的方法,涉及php基于curl的登陸及數(shù)據(jù)傳輸相關(guān)技巧,需要的朋友可以參考下2016-07-07基于session_unset與session_destroy的區(qū)別詳解
本篇文章是對(duì)php中session_unset與session_destroy的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php中通過(guò)虛代理實(shí)現(xiàn)延遲加載的實(shí)現(xiàn)代碼
基本原理是通過(guò)一個(gè)虛代理(Virtual Proxy)做占位符,一旦訪問(wèn)代理對(duì)象的某成員(方法或?qū)傩裕虞d就被觸發(fā)。2011-06-06Warning:?require():?open_basedir?restriction?in?effect,
在linux服務(wù)器部署thinkphp5的時(shí)候PHP報(bào)了Warning:?require():?open_basedir?restriction?in?effect這個(gè)錯(cuò)誤,是因?yàn)榫W(wǎng)站目錄配置錯(cuò)誤,PHP不能引入其授權(quán)目錄上級(jí)及其以上的文件。下面詳細(xì)講解如何處理這個(gè)問(wèn)題,需要的朋友可以參考下2022-11-11php輸出控制函數(shù)和輸出函數(shù)生成靜態(tài)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了php輸出控制函數(shù)和輸出函數(shù)生成靜態(tài)頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06PHP實(shí)現(xiàn)數(shù)組遞歸轉(zhuǎn)義的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組遞歸轉(zhuǎn)義的方法,包含了數(shù)組的遞歸調(diào)用與字符串的轉(zhuǎn)義方法,需要的朋友可以參考下2014-08-08