-1

I'm looking for a solution to flatten a variable multi-dimensionaled array to flatten all the values in the last available array to a single Array[] (containing the dataset of each last array in the multidimensional array).

Any help is appreciated!

It differs from other questions

Because the ending result should be a collection of all Arrays containing :

array(
    'title' => xx,
    'id' => xx
)

listed in the different multidimensional arrays. The different items all have fixed keys: title and id.

Sample Base array

$data = array(
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        array(
            array(
                'title' => xx,
                'id' => xx
            ),
            array(
                'title' => xx
                'id' => xx
            )
        )
    ),
    array(
        array(
            array(
                array(
                    'title' => xx,
                    'id' => xx
                ),
                array(
                    'title' => xx,
                    'id' => xx
                ),
                array(
                    'title' => xx,
                    'id' => xx
                )
            )
        ),
        array(
            'title' => xx,
            'id' => xx
        ),
        array(
            array(
                array(
                    array(
                        'title' => xx,
                        'id' => xx
                    )
                )
            )
        )
    )
);

Should be flattend to

 $data = array(
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    )
);
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
  • It's not a duplicate of that question .... That FLATTENS the array for each `key` entity ... I want the array of `key`/ `values` to be kept ... – daan.desmedt Oct 19 '17 at 13:42
  • There are [hundreds of "flatten array" questions](https://stackoverflow.com/search?q=%5Bphp%5D+flatten+array). What specifically have you tried, what are you stuck on and how does your problem differ from others? – deceze Oct 19 '17 at 13:50
  • http://idownvotedbecau.se/noattempt/ and http://idownvotedbecau.se/nocode/. Help yourself, we will look at your attempts. Read on foreach for the array, and is_array to know if the value is an array or a string. Go down on an array, keep the key and value otherwise. – Nic3500 Oct 19 '17 at 13:53

1 Answers1

1
function flatten_array(&$data) {

    foreach ($data as $index => &$item) {

        if(has_array_child($item)) {
            unset($data[$index]);
            flatten_array($item);
            $data = array_merge($data, $item);
        }

    }

}

function has_array_child($item) {
    foreach($item as $child) {
        if(is_array($child)) {
            return TRUE;
        }
    }

    return FALSE;
}

flatten_array($data);

print_r($data);
Mohammad Ali Akbari
  • 10,345
  • 11
  • 44
  • 62