views:

152

answers:

3

I have a messy tree multidimensional array that I want to do the following to:

Extract each array, no matter how far nested down to put it into a single "holder array", so this (just a basic example as it would be much more complex than this as far as the nesting)

$this = array[0]=> (array[1]=>('a','b'),
                    array[2]=>(array[3]=>('c','d')));

would become something like this, it doesn't matter if it changes the index for each array, just so that they are still in an array, but "flat" so the only nesting is within the one main holder array

$would_become = array[holder]=>(array[1]=>('a','b'),
                                array[2]=>(),
                                array[3]=>('c','d'));

The overall reasoning behind this is that I have a bunch of nested arrays that have a common key, such as ['filepath'] and I want to be able to do something like below (would need to make it go through each array in the holder array obviously, but this shows the basic idea behind why i need this.

foreach ($holder_array as $holder_array) {

// as an example:
echo $holder_array['Path']
}
A: 

in this link :

http://php.net/manual/en/function.array-values.php

a lot of examples .

what you want i think from your remark is like this example from the link above

i updated after comment :

<?php
    /* ---------------------
    * @function  array_flatten
    * @param     array
    * @since     0.1
    * @return    array
    * @notes     flatten associative multi dimension array recursive
    * @update    22:02 3/7/2009
    * @author    Rivanoor Bren <id_ivan(at)yahoo.com>
    ---------------------- */
    function array_flatten($array, $preserve = FALSE, $r = array()){
        foreach($array as $key => $value){
            if (is_array($value)){
                foreach($value as $k => $v){
                    if (is_array($v)) { $tmp = $v; unset($value[$k]); }
                }
                if ($preserve) $r[$key] = $value;
                else $r[] = $value;
            }
        }
        $r = isset($tmp) ? array_flatten($tmp, $preserve, $r) : $r;
        return $r;
    }

    print_r($tmp);
/* ---
Array
(
    [home] => Array
        (
            [id] => 1
            [pid] => 0
            [link] => home
            [subcat] =>
        )

    [works] => Array
        (
            [id] => 2
            [pid] => 0
            [link] => works
            [subcat] => Array
                (
                    [viz] => Array
                        (
                            [id] => 4
                            [pid] => 2
                            [link] => viz
                            [subcat] =>
                        )

                    [script] => Array
                        (
                            [id] => 5
                            [pid] => 2
                            [link] => script
                            [subcat] => Array
                                (
                                    [arch] => Array
                                        (
                                            [id] => 6
                                            [pid] => 5
                                            [link] => arch
                                            [subcat] =>
                                        )

                                )

                        )

                )

        )

    [blog] => Array
        (
            [id] => 3
            [pid] => 0
            [link] => blog
            [subcat] =>
        )

)

--- */

    print_r(array_flatten($tmp, 1));

/* ---
Array
(
    [home] => Array
        (
            [id] => 1
            [pid] => 0
            [link] => home
            [subcat] =>
        )

    [works] => Array
        (
            [id] => 2
            [pid] => 0
            [link] => works
        )

    [blog] => Array
        (
            [id] => 3
            [pid] => 0
            [link] => blog
            [subcat] =>
        )

    [viz] => Array
        (
            [id] => 4
            [pid] => 2
            [link] => viz
            [subcat] =>
        )

    [script] => Array
        (
            [id] => 5
            [pid] => 2
            [link] => script
        )

    [arch] => Array
        (
            [id] => 6
            [pid] => 5
            [link] => arch
            [subcat] =>
        )

)
--- */
?>
Haim Evgi
Maybe I'm interpreting the code you posted wrongly as I just glanced through it.. but I don't want a flat array, I know how to do that.. what I want is to preserve each array's values (but not its child arrays, as these would go into their own array) and put the values from an array into a new array in the holder array so each is preserved as being its own array.. essentially it is making each array equal to all others rather than in a tree structure,
Rick
I've been looking over a lot of examples but haven't found one that does this specifically and I'm not sure of how I can grab each specific array from within a multidimensional array as the array_walk_recursive, for example, seems to not have a way to pick out by the array but rather does so by key / value so they would all end up getting thrown into one flat array which makes my data not easily accessible in the manner I need it.
Rick
+2  A: 
function flatten(&$arr)
{
    foreach ( $arr as $k=>$v )
    {
        if ( is_array($v) )
        {
            flatten($arr[$k]);
            foreach ( $v as $kk=>$vv )
            {
                if ( is_array($vv) )
                {
                    unset($arr[$k][$kk]);
                    $arr[$kk] = $vv; // if you want the key to the array to be preserved.
                    // $arr[] = $vv; // would be safer, or check isset($arr[$kk]) first.
                }
            }
        }
    }
}

flatten($this[0]);
$would_become = array('holder'=>$this[0]);
mvds
thanks, I appreciate this, I didn't even get a chance to try this as the other one worked for me.. I appreciate the help in this
Rick
+1  A: 
<?php

$in = array(
        array('a','b'),
        array(
            array('c','d')
        )
);

function flatten($in) {
    $result = array();

    foreach ($in as $item) {
        if (is_array($item)) {
            $result[] = array_filter($item, 'notArray');
            $result = array_merge($result, flatten($item));
        } 
    }

    return $result;
}


function notArray($in) {
    return ! is_array($in);
}


$result = flatten($in);
print_r($result);
Tom Haigh
thanks, I really appreciate helping me with this.. it works great =)
Rick