[Solved] Flattening of multidimensional table in PHP


Try this:

function flatten_array($data) {
    $newArray = array();
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $newArray[] = 'Start' . $key;
            $newArray = array_merge($newArray,flatten_array($value));
            $newArray[] = 'End' . $key;
        } else {
            $newArray[$key] = $value;
        }
    }
    return $newArray;
}

$flat = flatten_array($data);

print_r($flat);

output:

Array
(
    [one] => one
    [0] => Starttwo
    [three] => three
    [four] => four
    [1] => Endtwo
    [five] => five
    [2] => Startsix
    [seven] => seven
    [3] => Starteight
    [nine] => nine
    [ten] => ten
    [4] => Endeight
    [5] => Endsix
)

solved Flattening of multidimensional table in PHP