开发者

PHP, recursive extract of all arrays in multidimensional array to one flat "holder" array?

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]=>开发者_StackOverflow中文版 (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']
}


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


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


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] =>
        )

)
--- */
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜