使用PHP从jstree获取JSON的嵌套值

时间:2017-03-22 11:06:34

标签: php json jstree

我使用jstree来使用JSON存储树结构。我的示例结构如下所示: enter image description here

使用后 $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以获取'text'值并拥有如下数组:

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

假设我不知道有多少级别。我试过像

这样的东西
foreach($treeJSONdecoded as $val){
   echo $val['text']; 
}

只是为了看看我能拿到什么,但似乎没有用。

1 个答案:

答案 0 :(得分:0)

谢谢@Scuzzy,这让我得到了我想要的东西:

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