[英]Get nested values from JSON from jstree with PHP
I use jstree to store a tree structure using JSON. 我使用jstree来使用JSON存储树结构。 My example structure looks like this: 我的示例结构如下所示:
After using $treeJSONdecoded = json_decode($treeJSON, true);
使用$treeJSONdecoded = json_decode($treeJSON, true);
my JSON looks like this: 我的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
)
How do I iterate through the whole JSON to get 'text' values and have an array like: 如何遍历整个JSON以获取“text”值并拥有如下数组:
{"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: 谢谢@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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.