2017-03-22 9 views
1

jstreeを使用してJSONを使用してツリー構造を保存します。私の例の構造は次のようになります。 enter image description herePHPでjstreeからJSONのネストされた値を取得

$treeJSONdecoded = json_decode($treeJSON, true); 私のJSONを使用した後は、次のようになります。

[0] => Array (
    [id] => j1_1 
    [text] => Release1 
    [icon] => 1 
    [li_attr] => Array (
     [id] => j1_1 
     ) 
    [a_attr] => Array (
     [href] => # 
     [id] => j1_1_anchor 
     ) 
    [state] => Array (
     [loaded] => 1 
     [opened] => 1 
     [selected] => 1 
     ) 
    [data] => Array () 
    [children] => Array (
     [0] => Array (
      [id] => j1_3 
      [text] => List of features 
      [icon] => 1 
      [li_attr] => Array (
       [id] => j1_3 
       ) 
      [a_attr] => Array (
       [href] => # 
       [id] => j1_3_anchor 
       ) 
      [state] => Array (
       [loaded] => 1 
       [opened] => 1 
       [selected] => 
       ) 
      [data] => Array () 
      [children] => Array (
       [0] => Array (
        [id] => j1_9 
        [text] => feature1 
        [icon] => 1 
        [li_attr] => Array (
         [id] => j1_9 
         ) 
        [a_attr] => Array (
         [href] => # 
         [id] => j1_9_anchor 
         ) 
        [state] => Array (
         [loaded] => 1 
         [opened] => 
         ) 
        [data] => Array () 
        [children] => Array () 
        [type] => default 
        ) 
       ) 
      [type] => default 
      ) 
     [1 => Array (id] => j1_2 
     [text] => List of documents 
     [icon] => 1 
     [li_attr] => Array (
      [id] => j1_2 
      ) 
     [a_attr] => Array (
      [href] => # 
      [id] => j1_2_anchor 
      ) 
     [state] => Array (
      [loaded] => 1 
      [opened] => 1 
      [selected] => 
      ) 
     [data] => Array () 
     [children] => Array (
      [0] => Array (
       [id] => j1_5 
       [text] => document1 
       [icon] => 1 
       [li_attr] => Array (
        [id] => j1_5 
        ) 
       [a_attr] => Array (
        [href] => # 
        [id] => j1_5_anchor 
        ) 
       [state] => Array (
        [loaded] => 1 
        [opened] => 
        ) 
       [data] => Array () 
       [children] => Array () 
       [type] => default 
       ) 
      ) 
     [type] => default) 
     ) 
    [type] => default 
    ) 

私は「テキスト」の値を取得するなど、配列を持つように全体JSONを反復処理するにはどうすればよいです:

{"Release1", "List of features", ... , "document1"} 

私はいくつのレベルがあるのか​​わからないと仮定します。

foreach($treeJSONdecoded as $val){ 
    echo $val['text']; 
} 

私はフェッチできるものを試してみましたが、うまくいかないようです。

+2

ルック_(笑)_、それは、ネストされた配列構造を紹介ループをするのに役立ちます。 – Scuzzy

+0

アレイの構造をよく見てください!配列の右側部分のforeachはちょうど見つけることができるでしょう – RiggsFolly

+1

ステートメント "私はいくつのレベルがあるかわからない_と訴えています"とは、再帰がある程度必要であることを意味します。 – Scuzzy

答えて

0

おかげ@Scuzzyが、これは私が望んで私を得た:RecursiveIterator/RecursiveIteratorIteratorに

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($treeJSON, TRUE)), 
    RecursiveIteratorIterator::SELF_FIRST); 

$newArray = []; 
foreach ($jsonIterator as $key => $val) { 
    if(!is_array($val) && $key == 'text') { 
     array_push($newArray, $val); 
    } 
} 
関連する問題