根据想要的JSON数据处理数组

_IronBear 2018-02-07 12:38:42
这个是无限极遍历出来的数组




这个是最终想要的json数据,里面的children,是用于orgchart做组织结构图的

这个好像只能按他的格式来,字段只要name和title

这里是要处理数组的json
"[{"id":1,"parentid":0,"name":"\u516c\u53f8\u7ea71","title":"","child":1,"children":[{"id":4,"parentid":1,"name":"\u73ed\u7ec4\u7ea71","title":"","child":1,"children":[{"id":5,"parentid":4,"name":"\u8f66\u95f4\u7ea71","title":"","child":0}]}]},{"id":2,"parentid":0,"name":"\u516c\u53f8\u7ea72","title":"","child":1,"children":[{"id":6,"parentid":2,"name":"\u73ed\u7ec4\u7ea72","title":"","child":1}]},{"id":3,"parentid":0,"name":"\u516c\u53f8\u7ea73","title":"","child":1,"children":[{"id":7,"parentid":3,"name":"\u73ed\u7ec4\u7ea73","title":"","child":1,"children":[{"id":8,"parentid":7,"name":"\u8f66\u95f4\u7ea72","title":"","child":0},{"id":9,"parentid":7,"name":"\u8f66\u95f4\u7ea73","title":"","child":0}]}]}]"

求大神帮忙处理下
...全文
1521 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2018-02-08
  • 打赏
  • 举报
回复
你贴出数据,我贴出代码
_IronBear 2018-02-08
  • 打赏
  • 举报
回复
引用 2 楼 moxioamo 的回复:
你倒是说你要干嘛啊
呃 就是想把第一图那个数组处理成下面图里children的json格式那样 因为他要的json格式的数据里只有name title和children这三个元素 我第一个数组里还有其他的,直接拿过来不显示
_IronBear 2018-02-08
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
不明白你要做什么
呃 就是想把第一图那个数组处理成下面图里children的json格式那样 因为他要的json格式的数据里只有name title和children这三个元素 我第一个数组里还有其他的,直接拿过来不显示
xuzuning 2018-02-08
  • 打赏
  • 举报
回复
不会递归?
$s =<<< TXT
[{"id":1,"parentid":0,"name":"\u516c\u53f8\u7ea71","title":"","child":1,"children":[{"id":4,"parentid":1,"name":"\u73ed\u7ec4\u7ea71","title":"","child":1,"children":[{"id":5,"parentid":4,"name":"\u8f66\u95f4\u7ea71","title":"","child":0}]}]},{"id":2,"parentid":0,"name":"\u516c\u53f8\u7ea72","title":"","child":1,"children":[{"id":6,"parentid":2,"name":"\u73ed\u7ec4\u7ea72","title":"","child":1}]},{"id":3,"parentid":0,"name":"\u516c\u53f8\u7ea73","title":"","child":1,"children":[{"id":7,"parentid":3,"name":"\u73ed\u7ec4\u7ea73","title":"","child":1,"children":[{"id":8,"parentid":7,"name":"\u8f66\u95f4\u7ea72","title":"","child":0},{"id":9,"parentid":7,"name":"\u8f66\u95f4\u7ea73","title":"","child":0}]}]}]
TXT;

$a = json_decode($s, 1);
$k = array('name', 'title', 'children');
foo($a, $k);
print_r($a);

function foo(&$ar, $key) {
  foreach($ar as &$v) {
    extract ($v);
    $v = compact($key);
    if(isset($v['children'])) foo($v['children'], $key);
  }
}

Array
(
    [0] => Array
        (
            [name] => 公司级1
            [title] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => 班组级1
                            [title] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => 车间级1
                                            [title] => 
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [name] => 公司级2
            [title] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => 班组级2
                            [title] => 
                        )

                )

        )

    [2] => Array
        (
            [name] => 公司级3
            [title] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => 班组级3
                            [title] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => 车间级2
                                            [title] => 
                                        )

                                    [1] => Array
                                        (
                                            [name] => 车间级3
                                            [title] => 
                                        )

                                )

                        )

                )

        )

)
594572148 2018-02-07
  • 打赏
  • 举报
回复
你倒是说你要干嘛啊
xuzuning 2018-02-07
  • 打赏
  • 举报
回复
不明白你要做什么

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧