Extract leaf nodes of a multidimensional array in PHP

Suppose I have an array in PHP that looks like

array
(
   array(0)
   (
       array(0)
       (
         .
         . 
         .
       )

       .
       .
       array(10)
       (
         ..
       )
   )

   .
   . 
   .
   array(n)
   (

     array(0)
     (
     )
   )
 )

And I need all the leaf elements of this mulitomeric array into a linear array, how should I do this without resorting to recursion, like this?

 function getChild($element)
 {

     foreach($element as $e)
     {

       if (is_array($e)
       {
          getChild($e);
       }
     }
 }

Note: the code snippet above is terribly incomplete

Update: Array Example

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Seller Object
                        (
                            [credits:private] => 5000000
                            [balance:private] => 4998970
                            [queueid:private] => 0
                            [sellerid:private] => 2
                            [dateTime:private] => 2009-07-25 17:53:10
                        )

                )

        )

... skipped.

[2] => Array
    (
        [0] => Array
            (
                [0] => Seller Object
                    (
                        [credits:private] => 10000000
                        [balance:private] => 9997940
                        [queueid:private] => 135
                        [sellerid:private] => 234
                        [dateTime:private] => 2009-07-14 23:36:00
                    )

            )

    ....snipped....

    )

)

+2
source share
6 answers

There is actually one function that will do the trick, check the manual page at: http://php.net/manual/en/function.array-walk-recursive.php

Quick snippet adapted from the page:

$data = array('test' => array('deeper' => array('last' => 'foo'), 'bar'), 'baz');

var_dump($data);

function printValue($value, $key, $userData) 
{
    //echo "$value\n";
    $userData[] = $value;
}


$result = new ArrayObject();
array_walk_recursive($data, 'printValue', $result);

var_dump($result);
+10
source

You can use iterators, for example:

$result = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::LEAVES_ONLY) as $value) {
    $result[] = $value;
} 
+6
source

:

function getLeafs($element) {
    $leafs = array();
    foreach ($element as $e) {
        if (is_array($e)) {
            $leafs = array_merge($leafs, getLeafs($e));
        } else {
            $leafs[] = $e;
        }
    }
    return $leafs;
}

. -, . , , :

function getLeafs($element) {
    $stack = array($element);
    $leafs = array();
    while ($item = array_pop($stack)) {
        while ($e = array_shift($item)) {
            if (is_array($e)) {
                array_push($stack, array($item));
                array_push($stack, $e);
                break;
            } else {
                $leafs[] = $e;
            }
        }
    }
    return $leafs;
}
+2

:

<?php

$data = array(array(array("foo"),"bar"),"baz");

$results = array();
$process = $data;
while (count($process) > 0) {
    $current = array_pop($process);
    if (is_array($current)) {
        // Using a loop for clarity. You could use array_merge() here.
        foreach ($current as $item) {
            // As an optimization you could add "flat" items directly to the results array here.
            array_push($process, $item);
        }
    } else {
        array_push($results, $current);
    }
}

print_r($results);

:

Array
(
    [0] => baz
    [1] => bar
    [2] => foo
)

, . , , PHP copy-on-write, zvals .

+2

, . , , , .

+1

Just got the same problem and used a different method that was not mentioned. A ArrayObjectclass is required to accept the correct answer . This can be done using a primitive arrayand a keyword usein an anonymous function (PHP> = 5.3):

<?php
$data = array(
    array(1,2,3,4,5),
    array(6,7,8,9,0),
);
$result = array();
array_walk_recursive($data, function($v) use (&$result) { # by reference
    $result[] = $v;
});
var_dump($result);
+1
source

All Articles