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

使用PHP从jstree的JSON中获取嵌套值,php,json,jstree,Php,Json,Jstree,我使用jstree来存储一个使用JSON的树结构。我的示例结构如下所示: 使用后 $treejsondecode=json_decode($treeJSON,true) 我的JSON如下所示: [0] => Array ( [id] => j1_1 [text] => Release1 [icon] => 1 [li_attr] => Array ( [id] => j1_1 ) [a_

我使用jstree来存储一个使用JSON的树结构。我的示例结构如下所示:

使用后
$treejsondecode=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']; 
}

只是想看看我能拿些什么,但它似乎不起作用。

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

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

查看RecursiveIterator/RecursiveIterator(lol),它将帮助您循环嵌套的数组结构!数组右侧的foreach可以工作,只要找到“假设我不知道有多少层”这句话,就意味着在某种程度上需要递归。查看RecursiveIterator/RecursiveIterator(lol),它将帮助您循环嵌套的数组结构。仔细查看数组结构!数组右侧的foreach将在find中工作“假设我不知道有多少层”的语句意味着在某种程度上需要递归。