1

多かれ少なかれオブジェクトのリストであるjsonフィードがあり、それぞれに独自のIDとidParentがあります。null の idParent を持つオブジェクトは、基本の親要素です。私が達成しようとしているのは、ツリー ビューのような適切な多次元配列を作成することです。子供も子供を持つことができることに注意してください。

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


}

誰かが助けを提供できれば、本当に感謝しています。

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 に答える