doucuodan0897 2016-09-16 13:50
浏览 23

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

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.

  • 写回答

2条回答 默认 最新

  • dqblm40280 2016-09-16 14:00
    关注

    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);
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度