phparray-walk

Get current index in array_walk function


$A = ["0" => "a", "1" => "b", "2" => "c"];
$B = ["0" => "aa", "1" => "bb", "2" => "cc"]
array_walk($A,function($item) use($B){
     $temp[] = $item;
     $temp[] = $B[?];
});

how to fill the ? above? how to get the current index in array_walk?


Solution

  • If I understood correctly something like that would work

    $A = ["0" => "a", "1" => "b", "2" => "c"];
    $B = ["0" => "aa", "1" => "bb", "2" => "cc"]
    array_walk($A,function($item, $key) use($B){
         $temp[] = $item;
         $temp[] = $B[$key];
    });