-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
    )
);
3
  • 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 ... Commented Oct 19, 2017 at 13:42
  • There are hundreds of "flatten array" questions. What specifically have you tried, what are you stuck on and how does your problem differ from others? Commented Oct 19, 2017 at 13:50
  • idownvotedbecau.se/noattempt and 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. Commented Oct 19, 2017 at 13:53

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, perfect! Thanks alot! As needed!
not necessary, $data passed by reference.
My bad, overlooked that one! Thanks again ..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.