-1
このコードを持っている:PHP - 配列 - 再帰のXpathとしてネストされた配列(木)の(リーフ/葉まで)各フルブランチを印刷
$text = '{"token_name":"C_ROOT","token_group":"C_BLOCK","group":true,"body":[[{"token_name_org":"T_VARIABLE","token":320,"value":"sort","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE"},{"token_name_org":"C_ASSIGNMENT_EQUAL","line":2,"value":"=","token":"VALUE","token_group":"ASSIGNMENTS"},{"token_name_org":"T_VARIABLE","token":320,"value":"_GET","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE","args":[[{"token_name_org":"T_CONSTANT_ENCAPSED_STRING","token":323,"value":"sort","line":2,"token_group":"STRINGS","token_name":"C_STRING"}]]}],[{"token_name_org":"T_VARIABLE","token":320,"value":"mort","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE"},{"token_name_org":"C_ASSIGNMENT_EQUAL","line":2,"value":"=","token":"VALUE","token_group":"ASSIGNMENTS"},{"token_name_org":"T_VARIABLE","token":320,"value":"_GET","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE","args":[[{"token_name_org":"T_CONSTANT_ENCAPSED_STRING","token":323,"value":"mort","line":2,"token_group":"STRINGS","token_name":"C_STRING"}]]}],[{"token_name_org":"T_VARIABLE","token":320,"value":"bort","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE"},{"token_name_org":"C_ASSIGNMENT_EQUAL","line":2,"value":"=","token":"VALUE","token_group":"ASSIGNMENTS"},{"token_name_org":"T_VARIABLE","token":320,"value":"_GET","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE","args":[[{"token_name_org":"T_CONSTANT_ENCAPSED_STRING","token":323,"value":"bort","line":2,"token_group":"STRINGS","token_name":"C_STRING"}]]}]]}';
$array = json_decode($text, TRUE);
// Collect the values here.
// Start with an empty string to force a leading '/' in the output
$path = array('');
// Walk the array, put the desired values in $path
array_walk_recursive(
$array,
function($value, $key) use (&$path) { // use reference to modify $path inside the function
if ($key == 'value') {
$path[] = $value;
}
}
);
// Join the collected values and output the result
echo(implode('/', $path));
をそれはアレイ(木)と印刷物を通過葉までの各ブランチのXPath、:JSONとして
清書配列:
{
"token_name": "C_ROOT",
"token_group": "C_BLOCK",
"group": true,
"body": [
[
{
"token_name_org": "T_VARIABLE",
"token": 320,
"value": "sort",
"line": 2,
"token_group": "VARIABLES",
"token_name": "C_VARIABLE"
},
{
"token_name_org": "C_ASSIGNMENT_EQUAL",
"line": 2,
"value": "=",
"token": "VALUE",
"token_group": "ASSIGNMENTS"
},
{
"token_name_org": "T_VARIABLE",
"token": 320,
"value": "_GET",
"line": 2,
"token_group": "VARIABLES",
"token_name": "C_VARIABLE",
"args": [
[
{
"token_name_org": "T_CONSTANT_ENCAPSED_STRING",
"token": 323,
"value": "sort",
"line": 2,
"token_group": "STRINGS",
"token_name": "C_STRING"
}
]
]
}
],
[
{
"token_name_org": "T_VARIABLE",
"token": 320,
"value": "mort",
"line": 2,
"token_group": "VARIABLES",
"token_name": "C_VARIABLE"
},
{
"token_name_org": "C_ASSIGNMENT_EQUAL",
"line": 2,
"value": "=",
"token": "VALUE",
"token_group": "ASSIGNMENTS"
},
{
"token_name_org": "T_VARIABLE",
"token": 320,
"value": "_GET",
"line": 2,
"token_group": "VARIABLES",
"token_name": "C_VARIABLE",
"args": [
[
{
"token_name_org": "T_CONSTANT_ENCAPSED_STRING",
"token": 323,
"value": "mort",
"line": 2,
"token_group": "STRINGS",
"token_name": "C_STRING"
}
]
]
}
],
[
{
"token_name_org": "T_VARIABLE",
"token": 320,
"value": "bort",
"line": 2,
"token_group": "VARIABLES",
"token_name": "C_VARIABLE"
},
{
"token_name_org": "C_ASSIGNMENT_EQUAL",
"line": 2,
"value": "=",
"token": "VALUE",
"token_group": "ASSIGNMENTS"
},
{
"token_name_org": "T_VARIABLE",
"token": 320,
"value": "_GET",
"line": 2,
"token_group": "VARIABLES",
"token_name": "C_VARIABLE",
"args": [
[
{
"token_name_org": "T_CONSTANT_ENCAPSED_STRING",
"token": 323,
"value": "bort",
"line": 2,
"token_group": "STRINGS",
"token_name": "C_STRING"
}
]
]
}
]
]
}
出力:
/sort/=/_GET/sort/mort/=/_GET/mort/bort/=/_GET/bort
誰かが、入れ子配列の数をどのように数えることができますか、いつ停止するか、分岐がどこで終了するかを知っていますか?また、 "ソート"だけを検索するように指定するには、 "mort"と "bort"の枝を表示しないでください。
出力は、検索「並べ替え」に基づいて、次のようになります。
/sort/=/_GET/sort
だから、擬似コードは、私はこのようなものだと思うようになります。
- は、キー「の値を持つ「身体」のアレイ内を検索します"検索と同じ(つまり、"ソート ")
- ブランチが終了するまで、すべての兄弟や子供、子供の子供などのキーの" value "値を取得します。
- 印刷枝次のボディアイテム
ありがとう
大きな説明、コメントとコード。ありがとう!!! –