10

I'm currently using array_map to apply callbacks to elements of an array. But I want to be able to pass an argument to the callback function like array_walk does. I suppose I could just use array_walk, but I need the return value to be an array like if you use array_map, not TRUE or FALSE.

So is it possible to use array_map and pass an argument to the callback function? Or perhaps make the array_walk return an array instead of boolean?

1

4 Answers 4

7

You don't need it to return an array.

Instead of:

$newArray = array_function_you_are_looking_for($oldArray, $funcName);

It's:

$newArray = $oldArray;
array_walk($newArray, $funcName);
Sign up to request clarification or add additional context in comments.

8 Comments

But if I use array_walk the return value will be TRUE of FALSE. I want the return value to be an array like how array_map does. So either find a way to pass an argument using array_map or find a way so that array_walk return array instead of boolean.
Does array_walk edit the original array on which it is called? Because if it does, this is pretty sweet.
that's because array_walk SETS the array. Try print_r($newArray); afterwards. Notice that Tomalak is NOT doing $arr = array_walk($newArray, $funcName); TRUE simply means 'it worked' and you can use the array that you inserted.
@sdleihssirhc Yes, the array_walk modify the original array. That is why I need it to return an array instead of boolean.
@PENDO Doesn't that kind of depend on the function that's supplied? I messed around with array_walk, and it only modified the original array if the "walker" function that I defined used the & symbol: function walker(&$val) { ... }
|
6

If you want return value is an array, just use array_map. To add additional parameters to array_map use "use", ex:

array_map(function($v) use ($tmp) { return $v * $tmp; }, $array);

or

array_map(function($v) use ($a, $b) { return $a * $b; }, $array);

Comments

2

Depending on what kind of arguments you need to pass, you could create a wrapped function:

$arr = array(2, 4, 6, 8);
function mapper($val, $constant) {
    return $val * $constant;
}

$constant = 3;
function wrapper($val) {
    return mapper($val, $GLOBALS['constant']);
}

$arr = array_map('wrapper', $arr);

This actually seems too simple to be true. I suspect we'll need more context to really be able to help.

Comments

0

To expand a bit on Hieu's great answer, you can also use the $key => $value pairs of the original array. Here's an example with some code borrowed from comment section http://php.net/manual/en/function.array-map.php

The following will use 'use' and include an additional parameter which is a new array.

Below code will grab "b_value" and "d_value" and put into a new array $new_arr (useless example to show a point)

// original array
$arr = ("a" => "b_value",
"c" => "d_value"); 

// new array
$new_arr = array();

array_map(function($k,$v) use (&$new_arr) { $new_arr[] = $v;}, array_keys($arr), $arr);

^ $k is key and $v is value

print_r of $new_arr is

Array
(
    [0] => b_value
    [1] => d_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.