Skip to Content Skip to Navigation Menu

Archives

My Amazon Wishlist

My Amazon.com Wish List
qrcode

Snippet: Php recursive flatten array

Posted Sunday the 15th of March, 2009 by Robert Carpenter

I wrote this nifty recursive function to flatten an array.

function array_flatten($a){ 
    $flat = array();
    if (count($a)==1) {
        return $a;
    }
    foreach ($a as $i=>$j){
        $new_flat_array = array_flatten($j);
        if (count($new_flat_array)>1) {
            foreach ($new_flat_array as $item) {
                array_push($flat, $item);
            }
        } else {
            array_push($flat,$j);
        }
    }
    return $flat;
}

/* It will take something like this: */

$unFlat = array("the quick","has","jumped","over",
   array("brown","fox",
      array(0=>"the",1=>"lazy"),
   "dog"),
".");
print_r($unFlat);

/*
Array
(
    [0] => the quick
    [1] => has
    [2] => jumped
    [3] => over
    [4] => Array
        (
            [0] => brown
            [1] => fox
            [2] => Array
                (
                    [0] => the
                    [1] => lazy
                )

            [3] => dog
        )

    [5] => .
)

// and turn it into this: */

print_r(array_flatten($unFlat));

/*
Array
(
    [0] => the quick
    [1] => has
    [2] => jumped
    [3] => over
    [4] => brown
    [5] => fox
    [6] => the
    [7] => lazy
    [8] => dog
    [9] => .
)

*/

No Comments Posted.

Post a comment:

You will be taken to a CAPTCHA in order to help curb spamming.
Privacy Policy

A random XKCD comic.

Schrodinger