1

我很茫然。我用谷歌搜索,在这里搜索,阅读 php 手册几个小时,但我无法提出解决方案。

我得到以下数组结构:

[x] => Array
    (
        ...
                  [object_name_1] => Array
                      (
                          [db] => x
                          [form] => y
                          [value] => z
                      )

                  ...

                  [object_name_4] => Array
                      (
                          [db] => x
                          [form] => y
                          [value] => z
                      )
                  ...

                  [object_name_n] => Array
                      (
                          [db] => x
                          [form] => y
                          [value] => z
                      )
...
[y] => Array
    (
        ...
        [object_group_xyz] => Array
           (
                  [object_name_a] => Array
                      (
                          [db] => x
                          [form] => y
                          [value] => z
                      )

                  ...
           )
                  ...

                  [object_name_e] => Array
                      (
                          [db] => x
                          [form] => y
                          [value] => z
                      )
                  ...

                  [object_name_z] => Array
                      (
                          [db] => x
                          [form] => y
                          [value] => z
                      )

现在我的问题如下:我想保存所有 object_name_x 数组。数组的深度未知,事实上,不同object_name_n对象的深度可以依赖于同一个数组。但它们永远是最后一片叶子。

我怎么解决这个问题。据我所知, RecursiveIteratorIterator 没有办法只考虑倒数第二个对象。

我的输出应该是这样的:

Array
(
    [object_name_1] => Array
        (
            [db] => x
            [form] => y
            [value] => z
        )
        ...
    [object_name_4] => Array
        (
            [db] => x
            [form] => y
            [value] => z
        )
        ...
    [object_name_n] => Array
        (
            [db] => x
            [form] => y
            [value] => z
        )
        ...
    [object_name_a] => Array
        (
            [db] => x
            [form] => y
            [value] => z
        )
        ...
    [object_name_e] => Array
        (
            [db] => x
            [form] => y
            [value] => z
        )
        ...
    [object_name_z] => Array
        (
            [db] => x
            [form] => y
            [value] => z
        )
        ...
)

感谢您的时间和帮助!

4

2 回答 2

2

它不是迭代器类的内置功能,但您仍然可以使用RecursiveIteratorIterator并构建一个堆栈,一旦您到达叶子,您就可以向后退一步。

以下更像是一个 PoC,它不是一个超级干净的解决方案,但它可以工作并且应该展示其原理:

例子

class MyIterator extends RecursiveIteratorIterator {
  protected $stack = array();
  protected $result = array();

  public function __construct($data)
  {
    parent::__construct(new RecursiveArrayIterator($data), RecursiveIteratorIterator::SELF_FIRST);
  }
  public function getObjects()
  {
    foreach($this as $dummy);
    return $this->result;
  }

  public function rewind()
  {
    parent::rewind();
    $this->stack = array();
    $this->result = array();
    $this->pushIfNoLeaf();
  }
  public function next()
  {
    parent::next();
    $this->pushIfNoLeaf();
  }
  public function beginIteration()
  {
    parent::beginIteration();
    $this->popIfLeaf();
  }
  public function beginChildren()
  {
    parent::beginChildren();
    $this->popIfLeaf();
  }
  protected function pushIfNoLeaf()
  {
    if ($this->getSubIterator()->hasChildren()) {
      $this->stack[] = array('key' => $this->key(), 'value' => $this->current());
    }
  }
  protected function popIfLeaf()
  {
    if (!$this->getSubIterator()->hasChildren()) {
      $item = array_pop($this->stack);
      $this->result[$item['key']] = $item['value'];
    }
  }
}
$data = array(
'obj1' => array(1,2,3),
'arr1' => array(
  'obj2' => array(4,5,6),
  'obj3' => array(7,8,9)
)
);

$iterator = new MyIterator($data);
var_dump($iterator->getObjects());

示例输出

array(3) {
  ["obj1"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  ["obj2"]=>
  array(3) {
    [0]=>
    int(4)
    [1]=>
    int(5)
    [2]=>
    int(6)
  }
  ["obj3"]=>
  array(3) {
    [0]=>
    int(7)
    [1]=>
    int(8)
    [2]=>
    int(9)
  }
}

演示

于 2013-03-31T18:35:50.177 回答
0

end然后prev像这样使用:

$array = array('one' => 1, 'two' => 2, 'three' => 3);

end($array);

echo prev($array); // Ouputs 2

echo key($array); // Ouputs 'two'

在您的情况下,您将拥有:

$array = array(

  0 => array(

    0 => (object) array('name' => 'a'),

    1 => (object) array('name' => 'b'),

    2 => (object) array('name' => 'c'),

  ),

  1 => array(

    3 => (object) array('name' => 'd'),

    4 => (object) array('name' => 'e'),

    5 => (object) array('name' => 'f'),

  ),

);

$objects = array();

foreach ($array as $a)
{

  end($a);

  $prev = prev($a);

  $objects[key($a)] = $prev;

}

echo '<pre>' . print_r($objects, TRUE) . '</pre>';

输出:

Array
(
    [1] => stdClass Object
        (
            [name] => b
        )

    [4] => stdClass Object
        (
            [name] => e
        )

)
于 2013-03-31T15:48:20.490 回答