Question

I am at a loss. I've googled, searched on here, read the php manual for hours but I cant come up with a solution.

I got the following array structure:

[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
                      )

Now my problem is the following: I want to save all object_name_x Arrays. The array is of unknown depth, as a matter of fact, the depth of the various object_name_n objects can depend in the same array. But they are always the last leaf.

How can I solve this problem. As far as I've found out, there is no way for RecursiveIteratorIterator to only consider the second to last objects.

My output should look like this:

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
        )
        ...
)

Thanks for your time and help!

Was it helpful?

Solution

It's not a built in feature of the iterator classes but you still could use the RecursiveIteratorIterator and build a stack where you can look one step back as soon as you reach a leaf.

The following is more of a PoC, it's not a super clean solution but it works and should demonstrate the principle:

Example

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());

Example Output

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)
  }
}

DEMO

OTHER TIPS

Use end then prev like so:

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

end($array);

echo prev($array); // Ouputs 2

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

In your case you'd have:

$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>';

Outputs:

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

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

)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top