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

时间:2013-10-01 20:02:56

标签: php arrays json multidimensional-array

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

{
    "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的根父节点。

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


}

这就是我正在尝试没有结果

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


}

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

1 个答案:

答案 0 :(得分:1)

我无法弄清楚你想要什么输出,所以我只做了一个简单的树结构:

$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

enter image description here