awebbdesigner Posted June 15, 2010 Share Posted June 15, 2010 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. Link to comment https://forums.phpfreaks.com/topic/204898-array-walk-with-variable-function-name-inside-a-class/ Share on other sites More sharing options...
Cagecrawler Posted June 15, 2010 Share Posted June 15, 2010 $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. Link to comment https://forums.phpfreaks.com/topic/204898-array-walk-with-variable-function-name-inside-a-class/#findComment-1072687 Share on other sites More sharing options...
awebbdesigner Posted June 15, 2010 Author Share Posted June 15, 2010 Many thanks, I will look into array_map Link to comment https://forums.phpfreaks.com/topic/204898-array-walk-with-variable-function-name-inside-a-class/#findComment-1072702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.