user4278933
user4278933

Reputation:

array_walk not doing the walk for me

When I pass an array as an extra argument to array walk, it does not get interpreted as I would expect.

function boom($item, $z )
{
    print_r("\n".$item);
    print_r("\n".$z);
}

$z=[ "alpha", "bravo" ];
$x=[ "one", "two" ];
array_walk( $x, 'boom', $z );

The output:

one
0 
two 
1

Why is zero and one printed? Why don't I get alpha and bravo printed? How can I get alpha and bravo included in the output?

Thanks!

Upvotes: 0

Views: 60

Answers (1)

Daniel Dudas
Daniel Dudas

Reputation: 3002

You have to change the code to pass $z as 3rd parameter like this:

function boom($item, $i, $z )
{
    print_r("\n".$item);
    print_r("\n".$z[$i]);
}

$z=[ "alpha", "bravo" ];
$x=[ "one", "two" ];
array_walk( $x, 'boom', $z );

You can find more here: http://php.net/manual/en/function.array-walk.php

If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback.

Upvotes: 1

Related Questions