在保持秩序的同时展平多维对象

I would like to flatten an object. This is what I've got so far:

{
    "1": {
        "id": 1,
        "name": "parent",
        "children": {
            "4": {
                "id": 4,
                "name": "child1",
                "parent": 1
            },
            "5": {
                "id": 5,
                "name": "child2",
                "parent": 1
            }
        }
    }, 
    "2":{
        "id": 2,
        "name": "parent2"
    }
}

And this is what I would like to accomplish. So keep the same order but flatten the object:

{
    "1": {
        "id": 1,
        "name": "parent",
    },
    "4": {
        "id": 4,
        "name": "child1",
        "parent": 1
    },
    "5": {
        "id": 5,
        "name": "child2",
        "parent": 1
    }, 
    "2": {
        "id": 2,
        "name": "parent2"
    }
}

So far I haven't found a solution to this. I've tried a function without much success:

protected function _flattenObject($array)
{
    static $flattened = [];
    if(is_object($array) && count($array) > 0)
    {
        foreach ($array as $key => $member) {
            if(!is_object($member))
            {
                $flattened[$key] = $member;
            } else
            {
                $this->_flattenObject($member);
            }
        }
    }
    return $flattened;
}

The tough part for me is to keep the same order (children below its parent). And the function mentioned above also removes all objects and almost only keeps the keys with its value, so it wasn't a great success at all.

Hopefully somebody over here knows a good solution for this.

By the way, the reason I want such flatten structure is because the system I have to work with, has trouble handling multidimensional arrays and objects. And I still want to display an hierarchy, which is possible with the flatten structure I described, because the objects actually contain a "level" key as well so I can give them some padding based on the "level" while still showing up below their parent.

EDIT: The JSON didn't seem to be valid, so I modified it a bit.

The main problem seems to be that you are not doing anything with the returned results of your recursive function. Unless using static inside a method does some magic that I don't know of...

So this section:

        if(!is_object($member))
        {
            $flattened[$key] = $member;
        } else
        {
            // What happens with the returned value?
            $this->_flattenObject($member);
        }

Should probably be more like this:

        if(!is_object($member))
        {
            $flattened[$key] = $member;
        } else
        {
            // Add the returned array to the array you already have
            $flattened += $this->_flattenObject($member);
        }

Here is code that works. It adds a field "level" to your objects, to represent how many levels deep in the original hierarchy they were.

<?php

$obj = json_decode('[{
    "id": 1,
    "name": "parent",
    "children": [{
        "id": 4,
        "name": "child1",
        "parent": 1
    }, {
        "id": 5,
        "name": "child2",
        "parent": 1
    }]
}, {
    "id": 2,
    "name": "parent2"
}]');


function _flattenRecursive($array, &$flattened, &$level)
{
    foreach ($array as $key => $member) {
        $insert = $member;
        $children = null;
        if (is_array($insert->children)) {
            $children = $insert->children;
            $insert->children = array();
        }
        $insert->level = $level;
        $flattened[] = $insert;
        if ($children !== null) {
            $level++;
            _flattenRecursive($children, $flattened, $level);
            $level--;
        }
    }
}

function flattenObject($array)
{
    $flattened = [];
    $level = 0;

    _flattenRecursive($array, $flattened, $level);

    return $flattened;
}

$flat = flattenObject($obj);

var_dump($flat);

?>