Example 1 — Visit Each Item
Print every key and value in an array.
<?php
$fruits = ["a" => "apple", "b" => "banana"];
array_walk($fruits, function ($value, $key) {
echo $key, " = ", $value, PHP_EOL;
}); How It Works
- Create an associative array of two fruits.
- array_walk() calls the function once for each item.
- The callback receives the value first and the key second.
- It prints each pair on its own line.

