In what scenarios would it be beneficial to use array_walk() function in PHP for array manipulation?
The array_walk() function in PHP can be beneficial when you need to apply a custom function to each element of an array without having to loop through it manually. This can save time and make the code more concise and readable. It is particularly useful when you need to perform the same operation on each element of an array, such as formatting or transforming the data.
// Example of using array_walk() to format each element of an array
$data = [10, 20, 30, 40, 50];
// Define a custom function to format the data
function formatData(&$value, $key) {
$value = '$' . number_format($value, 2); // Format as currency
}
// Apply the custom function to each element of the array
array_walk($data, 'formatData');
// Output the formatted data
print_r($data);