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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题