2

I have a array with n depth. i have tried this but its not working for me.

array_walk_recursive($old_value, function($v, $k, $u) use (&$values){
            if($k == "tour_id") {
               $values[] = $v;
            }
        },  $values );

here is my array:

array(
 "ID" => 1,
 "settings" => array("key" => 1,"scrum" => array("last_key" =>1, "past_key" => 12) )
) 

how to get past_key value efficiently.

2

1 Answer 1

2

You are almost there, but looking for the wrong key. Also the third argument passed to array_walk_recursive is passed as the third parameter to the callback (not needed for your needs, but I've added example use below).

<?php

$input = array(
 "ID" => 1,
 "settings" => array("key" => 1,"scrum" => array("last_key" =>1, "past_key" => 12) )
);

array_walk_recursive($input, function($v, $k, $needle_key) use (&$values){
    if($k == $needle_key) {
       $values[] = $v;
    }
},  'past_key');

var_dump($values);

Output:

array(1) {
    [0]=>
    int(12)
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Of course the third param, could alternatively be inherited by the closure.

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.