PHP-数组-递归将嵌套数组(Tree)的每个完整分支(最多叶/叶)打印为Xpath [英] PHP - Array - Recursively print each full branch (up to leaf/leaves) of the nested array (Tree) as an Xpath

查看:98
本文介绍了PHP-数组-递归将嵌套数组(Tree)的每个完整分支(最多叶/叶)打印为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,直到叶子为止:

It goes through the Array (Tree) and prints Xpath for each branch, up to the leaves:

将数组表示为JSON:

Prettified array as 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

任何人都知道如何计数嵌套数组的数量,知道何时停止,分支在何处结束?另外,如何指定仅搜索 sort,分支 mort和 bort应该不会出现。

Anybody have an idea how can I count the number of nested arrays, know when to stop, where the branch ends? Also how can I specify to search only for "sort", branches "mort" and "bort" should not appear.

输出应基于搜索 sort :

The output should be, based on search "sort":

/sort/=/_GET/sort

所以伪代码应该是这样的:

So the pseudocode should be I think something like this:


  1. 查找 body键为 value的数组等于要搜索的内容(即 sort)

  2. 获取所有兄弟姐妹和孩子,孩子的孩子等键为 value的值,直到分支结束。

  3. 打印分支

  4. 转到下一个正文项1

  1. Find in "body" array with key "value" equal to what is search for (i.e "sort")
  2. Get all the siblings and children, children's children etc key "value" value until the branch ends.
  3. Print the branch
  4. Goto 1 for next body item

谢谢

推荐答案

首先,您必须确定 body value =='sort'的$ c>然后应用现有代码(首先在函数中提取它):

First you have to identify that item of body that contains value == 'sort' then apply the existing code (extract it in a function first):

$search = 'sort';

// Identify the items having 'value' associated with $search
$allItems = array_filter(
    $array['body'],
    function (array $item) use ($search) {
        // $item contains many tokens, keep $item if the first token has value == 'sort'
        // just to be sure you can also check here if $item[0]['token'] is 320 or the value of 'token_org_name'
        return $item[0]['value'] == $search;
    }
);

// Pass all the found items to the function that produces the path
foreach ($allItems as $item) {
   echo(getPath($item)."\n");
}


function getPath(array $array) {
    // 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) {
            if ($key == 'value' ) {
                $path[] = $value;
            }
        }
    );

    // Join the collected values and return the result
    return implode('/', $path);
}

这篇关于PHP-数组-递归将嵌套数组(Tree)的每个完整分支(最多叶/叶)打印为Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆