Jump to content

Array walk with variable function name (inside a class)


awebbdesigner

Recommended Posts

I was just wondering whether it was possible to use array_walk on a function name that is variable...

or some other method for passing an array to a variable function

 

/// run the function

$func = "$this->$funD->afield";

array_walk($arg_array, "$func");

 

I googled around without much luck.

$array = array(1,2,3,4,5);
function addOne(&$value, $key){
$value = $value+1;
}
$func = 'addOne';
array_walk($array,$func);
var_dump($array);

 

Having the function name as a variable works in the above example.  There are a few things you should check:

- If you want to actually change the array you're passing, you'll need to pass the value by reference.

- If you are editing the array, the function should alter the value rather than returning the changed value.

 

You could also have a look at array_map if you only need the values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.