Thursday, 25 September 2014

array_walk_recursive in PHP

 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.

Syntax:

array_walk_recursive(array,function,parameter)
 
Parameters Description: 
array : Required. Specifying an array
function : Required. The name of the user-made function
parameter : Optional. Specifies a parameter to the user-made function

Tip: You can assign one parameter to the function, or as many as you like.

Example:

<?php 
function myfunc($val,$key)
{
echo "The key $key has the value $val ";
}
$daysarr1 = array( "a"=>"Sunday","b"=>"Monday" );
$daysarr2 = array($daysarr1,"1"=>"Tuesday","2"=>"Wednesday" );
array_walk_recursive($daysarr2,"myfunc");
?>

O/P:

The key a has the value Sunday 
The key b has the value Monday 
The key 1 has the value Tuesday
The key 2 has the value Wednesday

0 comments:

Post a Comment