1

I have a json feed that is more or less a list of objects each has it's own id and idParent. objects that have idParent of null are the base parent elements. What I'm trying to achieve is to make a proper multidimensional array like a tree view. Keep in mind that children can have children too.

{
    "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"
    }       
}

I've managed to filter out the root parents but after that I fail very bad The first function does filter out the root parent nodes with idParent of 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)));
    }


}

And this is what I'm experimenting on with no result

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


}

If anyone could provide some help I would really appreciate it.

4

1 回答 1

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

在此处输入图像描述

于 2013-10-01T21:12:56.877 回答