首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从json对象列表创建多维数组

从json对象列表创建多维数组
EN

Stack Overflow用户
提问于 2013-10-01 20:02:55
回答 1查看 1.9K关注 0票数 1

我有一个json提要,它或多或少是一个对象列表,每个对象都有自己的id和idParent。具有idParent为null的对象是基本父元素。我想要实现的是做一个像树视图一样的适当的多维数组。记住,孩子也可以有孩子。

代码语言:javascript
复制
{
    "obj1":{
        "idParent":null,
        "id":"parent1"
    },
    "obj2":{
        "idParent":null,
        "id":"parent2"
    },
    "obj3":{
        "idParent":null,
        "id":"parent3"
    },
    "obj4":{
        "idParent":null,
        "id":"parent4"
    },
    "obj5":{
        "idParent":null,
        "id":"parent5"
    },                      
    "obj6":{
        "idParent":"parent1",
        "id":"layer1-1"
    },
    "obj7":{
        "idParent":"parent1",
        "id":"layer1-2"
    },
    "obj8":{
        "idParent":"parent2",
        "id":"layer1-3"
    },
    "obj9":{
        "idParent":"parent4",
        "id":"layer1-4"
    },
    "obj10":{
        "idParent":"parent3",
        "id":"layer1-5"
    },                      
    "obj11":{
        "idParent":"layer1-1",
        "id":"layer2-1"
    },
    "obj12":{
        "idParent":"parent5",
        "id":"layer2-2"
    },
    "obj13":{
        "idParent":"layer1-4",
        "id":"layer2-3"
    },
    "obj14":{
        "idParent":"layer1-5",
        "id":"layer2-4"
    },
    "obj15":{
        "idParent":"layer1-5",
        "id":"layer2-5"
    }       
}

我已经成功地过滤掉了根父节点,但是在那之后我失败了,非常糟糕,第一个函数使用idParent为null过滤出根父节点。

代码语言:javascript
复制
function decodeData($data) {

    global $out;

    foreach ($data as $key => $obj) {
        if (is_array($obj)) {
            foreach ($obj as $prop => $value) {
                if ($prop == 'idParent') {
                    if($value == null) {
                        array_push($out, $obj);
                        unset($data[$key]);
                    }   
                }
            }
        }
    }

    if (count($data) > 0) {
        decodeData($data);
    } else {
        echo json_encode(array('length'=>count($data)));
    }


}

这就是我所做的实验,没有结果

代码语言:javascript
复制
function decodeData($arrays) {

    global $out;

    foreach ($arrays as $array_name => $arr) {
        foreach ($arr as $arr_prop => $arr_val) {
            if ($arr_prop == 'idParent' && $arr_val ==  null) { // we found root parents
                array_push($out, $arr);
                unset($arrays[$array_name]);    //remove array from the list
            } else { // check if idParent is inside out
                foreach ($out as $out_arr_name => $out_arr) { // iterate through out arrays
                    foreach ($out_arr as $out_arr_prop => $out_prop_val) { //
                        if ($out_arr_prop == 'id' && $arr_prop == 'idParent' && $out_arr_val == $arr_val) {
                            array_push($out_arr['children'], $obj);
                            unset($arrays[$array_name]);
                        }
                    }
                }
            }

        }
    }


    if (count($arrays) > 0) {
        decodeData($arrays);
    } else {
        echo json_encode(array('length'=>count($arrays)));
    }


}

如果有人能提供帮助,我会非常感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-01 21:12:56

我不知道你想要什么输出,所以我做了一个简单的树结构:

代码语言:javascript
复制
$data = json_decode( $your_json_string );

// Store each element in a lookup table indexed by element id
// 0th pass: put a fake root element there
$by_id = array(
  '*' => new stdclass
);

// First pass: put each element into there
foreach( $data as $o ) $by_id[ $o->id ] = $o;

// Second pass: add each element into its parent's children array
foreach( $data as $o ){
  $pid = $o->idParent ? $o->idParent : '*';
  $p = $by_id[ $pid ];
  $p->children[] = $o;
}

// Trash everything else, we start from the (fake) root element:
$tree = $by_id['*']->children;

/**** REVERSE ****/

$todo = $tree;
$json = array();

while( $todo ){
  $o = array_shift( $todo );
  if( isset( $o->children )){
    $todo = array_merge( $todo, $o->children );
    unset( $o->children );
  }
  $json[] = $o;
};

echo json_encode( $json );

结果:

http://codepad.viper-7.com/V7PjDh

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19124879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档