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

PHP foreach遍歷多維數(shù)組實(shí)現(xiàn)方式

 更新時(shí)間:2016年11月16日 14:18:42   作者:簡(jiǎn)單方式  
這篇文章主要為大家詳細(xì)介紹了PHP foreach遍歷多維數(shù)組實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

介紹
正常我們的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
   */

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論