When writing PHP applications, you may need to call a function once for each element in an array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_walk() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is array_walk() in PHP?
Call a function once for each element in an array.
Its syntax is:
array_walk(array|object &$array, callable $callback, mixed $arg = null): true
Parameters used by this operation:
&$array— The collection being examined or updated.$callback— A callable that controls the comparison or transformation; callbacks should follow the expected return contract.$arg— The value supplied for this argument affects the operation.
A callback needs a reference parameter to change each value. Do not structurally add or remove elements inside a walk callback.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$prices = ["pen" => 12, "book" => 25];
array_walk($prices, function (&$price, $name): void { $price *= 2; });
print_r($prices);
Output:
Array
(
[pen] => 24
[book] => 50
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$names = [" Maya ", " Leo "];
array_walk($names, function (&$name): void { $name = trim($name); });
print_r($names);
Output:
Array
(
[0] => Maya
[1] => Leo
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Normalize a list of incoming fields before further processing
Normalize a list of incoming fields before further processing. Save the code in a PHP file and adapt the sample data to your project.
<?php
$invoicePrices = ["book" => 100.0, "pen" => 20.0];
array_walk($invoicePrices, function (&$price): void { $price = round($price * 1.1, 2); });
print_r($invoicePrices);
Output:
Array
(
[book] => 110
[pen] => 22
)
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
A callback needs a reference parameter to change each value. Do not structurally add or remove elements inside a walk callback.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
array_walk() | Call a function once for each element in an array |
| Related functions | array_map() returns a transformed array; array_walk_recursive() visits nested leaves. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Forgetting the reference on a callback value | Use a reference parameter when the intention is to change the original array element. |
| Replacing array structure inside a callback | Use an explicit loop if you need to add or remove keys. |
| Confusing shallow and recursive traversal | A callback needs a reference parameter to change each value. Do not structurally add or remove elements inside a walk callback. |
Key Takeaways
array_walk()helps you call a function once for each element in an array.- A callback needs a reference parameter to change each value. Do not structurally add or remove elements inside a walk callback.
- array_map() returns a transformed array; array_walk_recursive() visits nested leaves.
- Use the practical example as a starting point, but validate real application inputs.
array_walk() is useful when you need to call a function once for each element in an array. Use its documented rules and the examples above to choose the right behavior for your data.