PHP foreach遍歷多維數(shù)組實(shí)現(xiàn)方式
介紹
正常我們的foreach可以按順序把一維數(shù)組里面每個(gè) key => value 打印出來,但是如果是多維數(shù)組則需要循環(huán)在嵌套循環(huán),或則遞歸實(shí)現(xiàn),但是這些方式都不夠靈活,因?yàn)樵诓淮_定該數(shù)組是幾維的情況下,不可能永無止境的嵌套循環(huán),如果采用遞歸到可以解決,但是如果只想使用foreach全部循環(huán)出來該如何實(shí)現(xiàn)?
實(shí)現(xiàn)方式 一
采用PHP本身自帶的迭代器類 RecursiveIteratorIterator
$test_arr = array(1,2,3,array(4,'aa'=>5,6,array(7,'bb'=>8),9,10),11,12);
$arrayiter = new RecursiveArrayIterator($test_arr);
$iteriter = new RecursiveIteratorIterator($arrayiter);
//直接打印即可按照橫向順序打印出來
foreach ($iteriter as $key => $val){
echo $key.'=>'.$val;
}
//結(jié)果
/*
0=>1
1=>2
2=>3
0=>4
aa=>5
2=>6
0=>7
bb=>8
4=>9
5=>10
4=>11
5=>12
*/
實(shí)現(xiàn)方式 二
自己實(shí)現(xiàn)一個(gè)類似于 RecursiveIteratorIterator 的迭代器類,實(shí)現(xiàn)多維數(shù)組橫向打印功能
class foreachPrintfArr implements Iterator {
//當(dāng)前數(shù)組作用域
private $_items;
private $_old_items;
//保存每次執(zhí)行數(shù)組環(huán)境棧
private $_stack = array();
public function __construct($data=array()){
$this->_items = $data;
}
private function _isset(){
$val = current($this->_items);
if (empty($this->_stack) && !$val) {
return false;
} else {
return true;
}
}
public function current() {
$this->_old_items = null;
$val = current($this->_items);
//如果是數(shù)組則保存當(dāng)前執(zhí)行環(huán)境,然后切換到新的數(shù)組執(zhí)行環(huán)境
if (is_array($val)){
array_push($this->_stack,$this->_items);
$this->_items = $val;
return $this->current();
}
//判斷當(dāng)前執(zhí)行完成后是否需要切回上次執(zhí)行環(huán)境
//(1) 如果存在跳出繼續(xù)執(zhí)行
//(2) 如果不存在且環(huán)境棧為空,則表示當(dāng)前執(zhí)行到最后一個(gè)元素
//(3) 如果當(dāng)前數(shù)組環(huán)境下一個(gè)元素不存在,則保存一下當(dāng)前執(zhí)行數(shù)組環(huán)境 $this->_old_items = $this->_items;
//然后切換上次執(zhí)行環(huán)境 $this->_items = array_pop($this->_stack) 繼續(xù)循環(huán), 直到當(dāng)前數(shù)組環(huán)境下一個(gè)
//元素不為空為止
while (1) {
if (next($this->_items)) {
prev($this->_items); break;
} elseif (empty($this->_stack)) {
end($this->_items); break;
} else {
end($this->_items);
if (!$this->_old_items)
$this->_old_items = $this->_items;
$this->_items = array_pop($this->_stack);
}
}
return $val;
}
public function next() {
next($this->_items);
}
public function key() {
// 由于 key() 函數(shù)執(zhí)行在 current() 函數(shù)之后
// 所以在 current() 函數(shù)切換執(zhí)行環(huán)境 , 會(huì)導(dǎo)致切換之前的執(zhí)行環(huán)境最后一個(gè) key
// 變成切換之后的key , 所以 $this->_old_items 保存一下切換之前的執(zhí)行環(huán)境
// 防止key打印出錯(cuò)
return $this->_old_items ? key($this->_old_items) : key($this->_items);
}
public function rewind() {
reset($this->_items);
}
public function valid() {
return $this->_isset();
}
}
內(nèi)部執(zhí)行方式
1、foreach 循環(huán)我們自定義的foreachPrintfArr類,會(huì)自動(dòng)調(diào)用內(nèi)部這5個(gè)方法 valid()、rewind()、key()、next()、current() 我們只需要實(shí)現(xiàn)這幾個(gè)方法即可.
2、調(diào)用順序:
第1次 => rewind -> valid -> current -> key
第2次~n次 => next -> valid -> current -> key
$test_arr = array(1,2,3,array(4,'aa'=>5,6,array(7,'bb'=>8),9,10),11,12);
$iteriter = new foreachPrintfArr($test_arr);
foreach ($iteriter as $key => $val){
echo $key.'=>'.$val;
}
//結(jié)果:
/*
0=>1
1=>2
2=>3
0=>4
aa=>5
2=>6
0=>7
bb=>8
4=>9
5=>10
4=>11
5=>12
*/
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP遍歷數(shù)組的方法匯總
- PHP遍歷數(shù)組的幾種方法
- PHP 數(shù)組遍歷方法大全(foreach,list,each)
- PHP循環(huán)遍歷數(shù)組的3種方法list()、each()和while總結(jié)
- 探討php中遍歷二維數(shù)組的幾種方法詳解
- PHP中多維數(shù)組的foreach遍歷示例
- PHP 數(shù)組遍歷foreach語法結(jié)構(gòu)及實(shí)例
- php實(shí)現(xiàn)遍歷多維數(shù)組的方法
- PHP中使用foreach()遍歷二維數(shù)組的簡單實(shí)例
- PHP遍歷數(shù)組的6種方式總結(jié)
相關(guān)文章
PHP中PDO連接數(shù)據(jù)庫中各種DNS設(shè)置方法小結(jié)
這篇文章主要介紹了PHP中PDO連接數(shù)據(jù)庫中各種DNS設(shè)置方法,結(jié)合實(shí)例形式總結(jié)分析了php常用的各種pdo連接數(shù)據(jù)庫技巧,需要的朋友可以參考下2016-05-05
ASP和PHP實(shí)現(xiàn)生成網(wǎng)站快捷方式并下載到桌面的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)生成網(wǎng)站快捷方式并下載到桌面的方法,比加入收藏、設(shè)為首頁更給力哦,需要的朋友可以參考下2014-05-05
PHP實(shí)現(xiàn)數(shù)組轉(zhuǎn)JSon和JSon轉(zhuǎn)數(shù)組的方法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組轉(zhuǎn)JSon和JSon轉(zhuǎn)數(shù)組的方法,結(jié)合實(shí)例形式分析了php數(shù)組與json相互轉(zhuǎn)換實(shí)現(xiàn)方法與操作技巧,需要的朋友可以參考下2018-06-06
php cURL和Rolling cURL并發(fā)方式比較
在實(shí)際項(xiàng)目或者自己編寫小工具(比如新聞聚合,商品價(jià)格監(jiān)控,比價(jià))的過程中, 通常需要從第3方網(wǎng)站或者API接口獲取數(shù)據(jù), 在需要處理1個(gè)URL隊(duì)列時(shí), 為了提高性能, 可以采用cURL提供的curl_multi_*族函數(shù)實(shí)現(xiàn)簡單的并發(fā)。2013-10-10
國外比較好的幾個(gè)的Php開源建站平臺(tái)小結(jié)
大量的PHP開源(開放源代碼/Open Source)應(yīng)用改變了這個(gè)世界,改變了互聯(lián)網(wǎng),以下我們總結(jié)從數(shù)據(jù)庫到購物、博客等眾多類型的開源PHP軟件,供網(wǎng)站開發(fā)者們參考。2010-04-04

