PHP array_walk function

Last Updated: February 17, 2022

array_walk — Iterate elements of the array and function are called for every element of the array. This does not work with deeper arrays (an array inside an array)

Syntax

array_walk_recursive(array|object &$array, callable $callback, mixed $arg = null): bool

Supports (PHP 4 , PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array target array
callback Callback function with two arguents. First one is the value and second one is the key

Return Value

Returns true on success or false on failure with values

Example

$array = array("one" => "One", "two" => "Two");


function test_print($item, $key)
{
    echo "$key holds $item". "\n";
}

array_walk($array, 'test_print');

Output

one holds One
two holds Two
Test