简体   繁体   中英

Get nested values from JSON from jstree with PHP

I use jstree to store a tree structure using JSON. My example structure looks like this: 在此输入图像描述

After using $treeJSONdecoded = json_decode($treeJSON, true); my JSON looks like this:

[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
    )

How do I iterate through the whole JSON to get 'text' values and have an array like:

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

assuming that I don't know how many levels there are. I tried something like

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

just to see what I can fetch but it doesn't seem to work.

Thanks @Scuzzy, this got me what I wanted:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM