PHP array walk recursive

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : array_walk_recursive() function

PHP: Apply a user function recursively to every member of an array

The array_walk_recursive() function apply a user defined function to every element of an array recursively. The user-defined function takes array's values and keys as parameters.

Version

(PHP 5 and above)

Syntax

array_walk_recursive(input_array, user_function, user_data)

Parameters

Name

Description

Required /
Optional

Type

input_array

The input array.

Required

Array

user_function

The name of the user-defined function.

Required

user_data

Additional parameter. If it is supplied, it will be passed as the third parameter to the user_function.

Optional

Mixed*

Return value

TRUE on success or FALSE on failure.

Value Type : Boolean

Pictorial Presentation :

php array array_walk_recursive() function

Example :

  1. function w3rfunction($value,$key)  

  2. {  

  3. echo "The key $key has the value $value
    ";  

  4. }  

  5. $tutorial1=array("x"=>"india","y"=>"Pakistan");  

  6. $tutorial2=array($tutorial1,"1"=>"China","2"=>"Japan");  

  7. array_walk_recursive($tutorial2,"w3rfunction");  

  8. ?>   

Output :

The key x has the value india
The key y has the value Pakistan
The key 1 has the value China
The key 2 has the value Japan