The array_walk() function applies a user-defined callback function to each element of an array. It returns TRUE on success and FALSE on failure, allowing developers to process array content flexibly.
<span class="fun">array_walk(arr, custom_func, parameter)</span>
The function returns TRUE on success and FALSE if an error occurs.
Below is an example demonstrating how to use the array_walk() function:
<?php
function display($val, $key) {
echo "$key id : Student $val<br>";
}
$arr = array("p" => "Tom", "q" => "Jack", "r" => "Amit");
array_walk($arr, "display");
?>
Running the above code will output:
p id : Student Tom
q id : Student Jack
r id : Student Amit
array_walk() is a very useful function for PHP array operations, suitable for batch processing of array elements with custom logic. Understanding its usage can significantly improve code flexibility and maintainability.