Array_walk_recursive in PHP


The array_walk_recursive() function runs each array element in a user-made function. The array’s keys and values are parameters in the function. The difference between this function and the array_walk() function is that with this function you can work with deeper arrays (an array inside an array). Returns True or False

In otherwords, Apply a user function recursively to every member of an array .

Syntax:

bool array_walk_recursive ( array &$input , callback $funcname [, mixed $userdata ] )

The array_walk_recursive() applies a user-defined callback function to every element of an array and will recurse into nested arrays. Normally the array_walk_recursive() function takes two arguments: the first one the array being walked over, and the second one the value of the key or index of the array. the third, otional argument is an additional value that you can send to the callback function. the Function returns true on success and false on failure.
If your call back function needs to be working with the actual values of the array, specify the first parameter of the callback as a reference. Then, any changes made to those elements will be made in the original array itself.

Example:

<html>
<body>
<div align=”center”>
<?php
$numbers=array(array(1,2,3,4),
array(4,8,10,12),
array(20,25,30,35),
);
function cube(&$element,$index)
{
print $index;
$element=$element*$element*$element;
}
?>
<table border=’1′><caption><font size=’-1′> The<em> array_walk_recursive()</em> function</font></caption>
<?php
array_walk_recursive($numbers,’cube’);
for($i=0;$i<3;$i++)
{
echo “<tr bgcolor=’999FFF’>”;
for($j=0;$j<4;$j++)
{
echo “<td><b>”.$numbers[$i][$j];
}
echo “</td></tr>”;
}
echo “<table>”;
?>
</div>
</body>
</html>

OUTPUT:

arrayfunction

Explain:
$numbers is a numeric array containing three arrays.
This callback function takes a reference to the array being walked over as its first argument and $key, which is the index of each element of the array. Its function is to walk through the array cubing each of its elements.
The array_walk_recursive() function takes the array as its first argument, and the name of the function, ‘cube’, as a string value for its second argument. The cube() function is the callback function that will be applied toeach element of the two-dimensional array, $numbers.

Bookmark and Share
  1. Hello colleagues, how is the whole thing, and what you would like to
    say on the topic of this piece of writing, in
    my view its genuinely awesome designed for me.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: