PHP has some great functions (like array_walk) that allow you to process each element in an array. They’re generally set up so you specify the array you want processed as the first parameter and a callback function to apply to each element as the second. These functions return booleans indicating success, not a copy of the modified array as you might expect. If you want the array to be modified, you have to pass the array in by reference like array_walk(&$my_array, 'my_callback');
However, in PHP 5.3 and above, if you pass by reference to function call you get a E_DEPRECATED error.
Does anyone know (if there exists) a correct way to use these functions to modify arrays without triggering the errors and without explicitly suppressing them? Are there newer alternatives to these old array processing functions.
Values are passed by reference implicitly in PHP >= 5.3 as determined by the function definition.
Function definition for
array_walk():Note
&$array. As such, you do not need to explicitly pass the array by reference in the function call in PHP >= 5.3.However, you will need to ensure that the callback accepts it’s value by reference accordingly (as demonstrated by nickb).
Also take a look at PHP 5.4 Call-time pass-by-reference – Easy fix available?