0

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,

6
  • how can the output be /sort/=/_GET/sort when you only search for the one array having value='sort'? Commented Jun 22, 2017 at 10:27
  • and the function doesn't return anything, so the last line will not print anything Commented Jun 22, 2017 at 10:29
  • I'm struggling to see how you wrote a recursive function and were then unable to select the keys and values. Is this a homework question? Commented Jun 22, 2017 at 10:29
  • @axiac yes that's correct. I would like to output all the values key value seperated by "/" including sibligns childs, and child's child etc Commented Jun 22, 2017 at 10:30
  • The array posted in the question defines only one path (/sort/=/_GET/sort). How should the function work if multiple paths are possible? For example, if the content of body is duplicated and the values associated with the value key are modified? Commented Jun 22, 2017 at 10:31

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

4 Comments

Almost perfect answer!!!! Thanks!! Wouldnt figure it out. Just small question: How to make that each Path is displayed in new line i.e Now it is: "/sort/=/_GET/sort/mort/=/_GET/mort" need it to be "/sort/=/_GET/sort\n/mort/=/_GET/mort"
Also if you read this, a search key value should be also a parameter i.e I want to do this only for "sort", when first element has key "value" with value "sort" it than should build the path till the end, others should be ignored
Hard to explain this :) Thanks for your help anyway. Got me further
Openend new question for this: stackoverflow.com/questions/44699113/…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.