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.