douqie1884 2017-06-22 10:16
浏览 33
已采纳

PHP - 数组 - 基于键访问兄弟/子键值

Have this 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"
            }
          ]
        ]
      }
    ]
  ]
}

Wrote this code to search for key "value" being "sort".

    public function search_var($array,$var)
    {


        foreach($array as $key=>$value)
        {

            if(is_array($value))
            {

                $this->search_var($value,$var);

            }else{
                if(isset($array["value"]) && $array["value"] == $var)
                {
                    print $value."
";
                }   


            }

        }

    }

  print_r($scanner->search_var($map,"sort"));

Don't know how can I reference in my code the siblings and childs? I.e

Now the output is:

T_VARIABLE
320
sort
2
VARIABLES
C_VARIABLE

How can I make it that I see as output only:

/sort/=/_GET/sort

Each value between "/" is a key "value" in sibling or child (last case)

Thanks,

  • 写回答

2条回答 默认 最新

  • dongrang9300 2017-06-22 10:43
    关注

    It's not clear from the question what to return when body contains more information than the one posted in the question. This answers assumes it never does.

    A possible solution is to use array_walk_recursive() and collect the values associated with the value keys into an array. After the walk, the collected values are simply joined using the desired separator (/):

    $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"}]]}]]}';
    
    $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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了