PHP Tutorial - PHP array_walk_recursive() Function






Definition

The array_walk_recursive() function runs a user-defined function on each array element including inner array.

Syntax

PHP array_walk_recursive() Function has the following syntax.

array_walk_recursive(array,myfunction,parameter...)

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to walk on
myfunctionRequired.The name of the user-defined function
parameter,...Optional.Parameter to the user-defined function. You can assign one parameter to the function, or as many as you like.

Example

Run user defined function on array element


<?php
    function myfunction($value,$key){
        echo "The key $key has the value $value<br>";
    }
    $a1=array("a"=>"PHP","b"=>"java2s.com");
    $a2=array($a1,"1"=>"One","2"=>"Two");
    array_walk_recursive($a2,"myfunction");
?>

The code above generates the following result.