PHP - Recursive Multidimension Array iterator

前端 未结 3 2131
野趣味
野趣味 2020-12-16 04:11

I\'m trying to write a recursive array iterator function in which the function will return a result set of all sets that are specified by \'$needle\'. Where $needle = key

相关标签:
3条回答
  • 2020-12-16 04:37

    A tiny modification of your construction:

    $holder = recursive($needle, $value, $holder);

    Ay?

    0 讨论(0)
  • 2020-12-16 04:40

    More fine-grained control is perhaps possible with true (tm) recursive array traversal via RecursiveIterator interface and some key filters and array conversion functions:

    $needle = '0';
    $array  = [[1]];
    
    $it = new KeyFilter(
        new RecursiveIteratorIterator(
            new MyRecursiveArrayIterator($array)
            , RecursiveIteratorIterator::SELF_FIRST
        )
        , $needle
    );
    
    $result = iterator_to_array($it, FALSE);
    var_dump($result);
    

    Providing an exemplary result as:

    array(2) {
      [0] =>
      array(1) {
        [0] =>
        int(1)
      }
      [1] =>
      int(1)
    }
    

    Full code example (Demo):

    <?php
    /**
     * @link http://stackoverflow.com/q/19709410/367456
     */
    
    Class MyRecursiveArrayIterator extends ArrayIterator implements RecursiveIterator
    {
        public function hasChildren()
        {
            $current = $this->current();
            return is_array($current) && count($current);
        }
    
        public function getChildren()
        {
            return new self($this->current());
        }
    }
    
    class KeyFilter extends RegexIterator
    {
        public function __construct(Iterator $iterator, $key)
        {
            parent::__construct(
                $iterator, '/' . preg_quote($key) . '/', NULL, RegexIterator::USE_KEY
            );
        }
    }
    
    $needle = '0';
    $array  = [[1]];
    
    $it = new KeyFilter(
        new RecursiveIteratorIterator(
            new MyRecursiveArrayIterator($array)
            , RecursiveIteratorIterator::SELF_FIRST
        )
        , $needle
    );
    
    $result = iterator_to_array($it, FALSE);
    var_dump($result);
    
    0 讨论(0)
  • 2020-12-16 04:46

    You don't need to reinvent the wheel since PHP has standard Recursive Iterator API:

    //$array is your multi-dimensional array
    $result   = [];
    $search   = 'foo';
    $iterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator(
            $array,
            RecursiveArrayIterator::CHILD_ARRAYS_ONLY
        )
    ); 
    
    foreach($iterator as $key=>$value)
    {
       if($search==$key && $value!=='')
       {
          $result[] = $value;
       }
    }
    

    -note, that, since you're searching for value by key - in common case $value will hold entire subsection.

    If you want to do this in your own recursive function, here's one:

    function recursive($needle, $array, $holder = []) 
    {
        $holder = [];
        foreach($array as $key=>$value)
        {
           if($key===$needle && $value!=='')
           {
              $holder = array_merge($holder, [$value]);
           }
           if(is_array($value))
           {
              $holder = array_merge($holder, recursive($needle, $value, $holder));
           }
        }
        return $holder;
    }
    
    0 讨论(0)
提交回复
热议问题