dev
dev

Reputation: 1179

PHP - Array - Access siblings/child key values based on key

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."\n";
                }   


            }

        }

    }

  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,

Upvotes: 0

Views: 336

Answers (2)

axiac
axiac

Reputation: 72256

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));

Upvotes: 1

Philip Couling
Philip Couling

Reputation: 14913

Your question is very badly worded but I believe you are trying to achieve this:

public function search_var($array,$var) {
    foreach($array as $key=>$value) {
        if(is_array($value)) {
            $this->search_var($value,$var);
        } elseif($key == $var) {
            print "/".$value;
        }
    }
}

Upvotes: 0

Related Questions