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.

Coder
  • 184
  • 15

1 Answers1

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)
  }
Progrock
  • 7,373
  • 1
  • 19
  • 25