PHP, a recursive extract of all arrays in a multidimensional array into one flat holder array?

I have a multidimensional messy tree array that I want to do as follows:

Extract each array, no matter how far it is nested, to fit it into a single “holder array”, so that’s (just a basic example, since it would be much more complicated than it would be when nested)

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

will become something like this, it doesn’t matter if it changes the index for each array, just so that they are still in the array, but “flat”, so the only attachment is within the same main array of holders

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

The general argument for this is that I have a lot of nested arrays that have a common key, for example ['filepath'], and I want to be able to do something like below (I would have to go through each array in the holders array obviously, but this shows the basic idea why I need it.

foreach ($holder_array as $holder_array) {

// as an example:
echo $holder_array['Path']
}
+3
source share
3 answers
<?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);
+2
source
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]);
+2
source

:

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

.

, , ,

:

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

Source: https://habr.com/ru/post/1758896/


All Articles