array_walk_recursive


php128 apg

APPLY Apply a user supplied function to every member of an ARRAY, recursively.





The $callback has typically two parameters.

The $array parameter's value being the first, and the key/index second.

If $callback needs to be working with the actual values of the array, specify the first parameter of $callback as a reference.

Then, any changes made to those elements will be made in the original array itself.

If the optional $userdata is supplied, it will be passed as the third parameter to the $callback.

This function returns TRUE on success or FALSE on failure.



<?php

bool array_walk_recursive 
( arr &$array , callable $callback [, 
                                                                   
mix $userdata = NULL ] )
 

where,

&
$array = The input ARRAY

$callback = The function to be used as the user-defined function
                        
$userdata = The third parameter to be passed to the callback function

?> 

&$array


The input ARRAY.



$callback


The function to be used as the user-defined function.



$userdata


The third parameter to be passed to the callback function.



  1 EXERCISE   

<?php

function comp01($val01, $key01, $nmr01)
{
echo 
$nmr01 . '&nbsp;&nbsp;' . $val01 . 
                       
'&nbsp;&nbsp;(' . $key01 .  ')<br>';
}

$arr01 = [ "R" => "RED", 
                
"G" => "GREEN", 
                
"B" => "BLUE", 
        
"WHITE" => [ 255,255,255 ] ];

echo 
'<pre>';
print_r($arr01);
echo 
'</pre>';

@
array_walk($arr01, "comp01", "Component: ");

echo 
'<pre>';
print_r($arr01);
echo 
'</pre>';

echo 
'<br><br>';

@
array_walk_recursive($arr01, "comp01", "Component: ");

echo 
'<pre>';
print_r($arr01);
echo 
'</pre>';

?> 

 RESULT   

INITIAL ARRAY:
Array
(
[R] => RED
[G] => GREEN
[B] => BLUE
[WHITE] => Array
(
[0] => 255
[1] => 255
[2] => 255
)

)

After array_walk:
Component: RED (R)
Component: GREEN (G)
Component: BLUE (B)
Component: Array (WHITE)


After array_walk_recursive:
Component: RED (R)
Component: GREEN (G)
Component: BLUE (B)
Component: 255 (0)
Component: 255 (1)
Component: 255 (2)


  2 EXERCISE   

<?php

function foo($value) {
    echo 
$value . " foo<br>";
}

function 
bar($value) {
    echo 
$value . " bar<br>";
}

$arr = array (1,2,3);
var_dump (array_walk_recursive ($arr, 'foo'));
echo 
'<br><br>';
var_dump (array_walk_recursive ($arr, 'bar'));

?>

  3 EXERCISE   

<?php

echo "Testing array_walk_recursive() : basic functionality.";

// regular array
$fruits = array("lemon", array("orange", "banana"), 
                                     array(
"apple"));

function 
test_print($item, $key)
{
   
// dump the arguments to check that they are passed
   // with proper type
   
var_dump($item);
   
// value
   
var_dump($key);
   
// key
   
echo "<br><br>";
   
// new line to separate the 
   // output between each element
}
function 
with_userdata($item, $key, $user_data)
{
   
// dump the arguments 
   // to check that they are passed
   // with proper type
   
var_dump($item);
   
// value
   
var_dump($key);
   
// key
   
var_dump($user_data);
   
// user supplied data
   
echo "<br><br>";
   
// new line to separate the 
   // output between each element
}

echo 
"<br><br><br>With default parameters 
                            to show array contents:<br><br>"
;
var_dump( array_walk_recursive($fruits, 'test_print'));

echo 
"<br><br><br>With all parameters:<br><br>";
var_dump( array_walk_recursive($fruits, 
                                        
'with_userdata', 
                                        
"Added"));

?>

  4 EXERCISE   

<?php
echo "Testing array_walk_recursive() : basic functionality.";

// associative array
$fruits = array("a" => "lemon", 
                     
"b" => array( "c" => "orange", 
                                    
"d" => "banana"), 
                                    
"e" => array("f" => "apple"));
                                    
echo 
'<br><br>The given $fruits:<br>';
var_dump($fruits);

function 
test_alter(&$item, $key, $prefix)
{
  
// dump the arguments to check that they are passed
  // with proper type
  
var_dump($item);
  
// value
  
var_dump($key);
  
// key
  
var_dump($prefix);
  
// additional argument passed 
  // to callback function
  
echo "<br><br>";
  
// new line to separate the 
  // output between each element
}

function 
test_print($item, $key)
{
  
// dump the arguments to 
  // check that they are passed
  // with proper type
  
var_dump($item);
  
// value
  
var_dump($key);
  
// key
  
echo "<br><br>";
  
// new line to separate the 
  // output between each element
}

echo 
"<br><br><br>With default parameters 
                             to show array contents:<br><br>"
;
var_dump(array_walk_recursive($fruits, 'test_print'));

echo 
"<br><br><br>With one optional parameter 
                       to modify contents:<br><br>"
;
var_dump (array_walk_recursive($fruits, 'test_alter', 'fruit'));

echo 
"<br><br><br>With default parameters 
                       to show modified array contents:<br><br>"
;
var_dump (array_walk_recursive($fruits, 'test_print'));

?>

  5 EXERCISE   

<?php

/*
 * Testing array_walk_recursive() 
 * with following types of 'input' arrays:
 *    integer, float, string, bool, 
 *    null, empty & mixed
*/

function print_value($value, $key, $count)
{
  echo  
$count." : ".$key." ".$value."\n";
}

echo 
"With different values.";

// different arrays as input
$input_values = array(

       
// integer values
/*1*/  
array(array(1, 0, -10), array(023, -041), 
                                            array(
0x5A, 0X1F, -0x6E)),

       
// float value
       
array(array(3.4, 0.8, -2.9), array(6.25e2, 8.20E-3)),

       
// string values
       
array('Mango', array("Apple", 'Orange', "Lemon")),

       
// bool values
/*4*/  
array( array(true, false), array(TRUE, FALSE)),

       
// null values
       
array( array(null), array(NULL)),

       
// empty array
       
array(),

       
// binary array
       
array(array(b'binary')),

       
// mixed array
/*8*/  
array(16, 8.345, array("Fruits"), 
                                   array(
true, null), 
                                   array(
FALSE), 
                                   array(-
98, 0.005, 'banana'))
);

for(
$count = 0; $count < count($input_values); $count++) {
  echo 
"<br><br>Iteration: ".($count + 1)."<br>";
  
var_dump( array_walk_recursive($input_values[$count], 
                                                                 
"print_value", 
                                                                  
$count+1));
}

?>

  6 EXERCISE   

<?php

/*
 * Testing array_walk_recursive() 
 * with an array having subarrays as elements
*/

echo "Testing array_walk_recursive() : array with subarray.<br><br>";

function 
callback($value, $key)
{
   
// dump the arguments to check that they are passed
   // with proper type
   
var_dump($key);
   
// key
   
var_dump($value);
   
// value
   
echo "<br><br>";
   
// new line to separate the 
   // output between each element
}

$input = array(
  array(),
  array(
1),
  array(
1,2,3),
  array(
"Mango", "Orange"),
  array(array(
1, 2, 3), array(1))
);

var_dump( array_walk_recursive( $input, "callback"));

?>

  7 EXERCISE   

<?php
/*
 * Testing array_walk_recursive() 
 * with an array having reference variables
*/

echo "Testing array_walk_recursive() : array with references.<br><br>";

$value1 = 10;
$value2 = -20;
$value3 = &$value1;
$value4 = 50;

// 'input' array containing references 
// to above variables
$input = array(&$value1, array(&$value2, -35), 
                                        array(&
$value3, 0), 
                                        array(&
$value4));

function 
callback($value, $key)
{
   
// dump the arguments to check 
   // that they are passed
   // with proper type
   
var_dump($key);
   
// key
   
var_dump($value);
   
// value
   
echo "<br><br>";
   
// new line to separate the 
   // output between each element
}

var_dump( array_walk_recursive($input, "callback"));

?>

  8 EXERCISE   

<?php

/*
 * Passing 'input' argument 
 * as an associative array
 * with Numeric & string keys
*/

echo "Testing array_walk_recursive() : 
                   'input' as an associative array."
;

function 
for_numeric($value, $key, $user_data)
{
  
// dump the input values to see if they are
  // passed with correct type
  
var_dump($key);
  
var_dump($value);
  
var_dump($user_data);
  echo 
"<br><br>";
  
// new line to separate the output 
  // between each element
}

function 
for_string($value, $key)
{
  
// dump the input values to see if they are
  // passed with correct type
  
var_dump($key);
  
var_dump($value);
  echo 
"<br><br>";
  
// new line to separate the output
  // between each element
}

function 
for_mixed($value, $key)
{
  
// dump the input values 
  // to see if they are
  // passed with correct type
  
var_dump($key);
  
var_dump($value);
  echo 
"<br><br>";
  
// new line to separate the output
  // between each element
}

// Numeric keys
$input = array( 0 => array(1 => 25, 5 => 12, 0 => -80), 
                                         
1 => array(-2 => 100, 5 => 30));
echo 
"<br><br><br>Associative array with numeric keys:<br><br>";
var_dump( array_walk_recursive($input, "for_numeric", 10));

// String keys
$input = array( "a" => "Apple", 
                 
'z' => array('b' => 'Bananna', "c" => "carrot"), 
                 
'x' => array('o' => "Orange"));
echo 
"<br><br><br>Associative array with string keys:<br><br>";
var_dump( array_walk_recursive($input, "for_string"));

// binary key
$input = array( b"a" => "Apple", b"b" => "Banana");
echo 
"<br><br><br>Associative array with binary keys:<br><br>";
var_dump( array_walk_recursive($input, "for_string"));

// Mixed keys - numeric/string
$input = array( 0 => array(0 => 1, 1 => 2), 
                       
"x" => array("a" => "Apple", "b" => "Banana"), 
                        
2 =>3);
echo 
"<br><br><br>Associative array with numeric/string keys:<br><br>";
var_dump( array_walk_recursive($input, "for_mixed"));

?>

  9 EXERCISE   

<?php

/*
* Passing anonymous(run-time) callback 
* function with following variations:
*   with one parameter
*   two parameters
*   three parameters
*   extra parameters
*   without parameters
*/

echo "Testing array_walk_recursive() : 
                 anonymous function as callback."
;

$input = array( array(2, 5), array(10, 0));

echo 
"<br><br><br>With one argument:<br><br>";
var_dump( array_walk_recursive($input, 
                  function(
$value) { var_dump($value); 
                  echo 
"<br>"; }));

echo 
"<br><br><br>With two arguments:<br><br>";
var_dump( array_walk_recursive($input, 
               function(
$value, $key) 
               { 
var_dump($key); var_dump($value); 
               echo 
"<br>"; }
                              ));

echo 
"<br><br><br>With three arguments:<br><br>";
var_dump( array_walk_recursive($input, function($value, 
                         
$key, $user_data) { var_dump($key); 
                                                   
var_dump($value); 
                                                   
var_dump($user_data); 
                                                   echo 
"<br>"; }, 10));

echo 
"<br><br><br>With null argument:<br><br>";
var_dump( array_walk_recursive( $input, function() 
                                 { echo 
"1<br>"; }
                                                  ));

?>

  10 EXERCISE   

<?php

/*
 * Passing different types of callback 
 * functions to array_walk_recursive()
 *   without parameters
 *   with less and more parameters
*/

echo "Testing array_walk_recursive() : 
                     callback function variation."
;

$input = array(array('Apple', 'Banana'), 
                                   
'Mango', 
                                      array(
'Orange'));

echo 
"<br><br><br>callback function 
                          with both parameters:<br><br>"
;
function 
callback_two_parameter($value, $key)
{
   
// dump the arguments to check 
   // that they are passed
   // with proper type
   
var_dump($key);
   
// key
   
var_dump($value);
   
// value
   
echo "<br><br>";
   
// new line to separate the 
   // output between each element
}
var_dump( array_walk_recursive($input, 
                            
'callback_two_parameter'));

echo 
"<br><br><br>callback function 
                         with only one parameter:<br><br>"
;
function 
callback_one_parameter($value)
{
   
// dump the arguments to check 
   // that they are passed
   // with proper type
   
var_dump($value);
   
// value
   
echo "<br><br>";
   
// new line to separate the 
   // output between each element
}
var_dump( array_walk_recursive($input, 
                               
'callback_one_parameter'));

echo 
"<br><br><br>callback function 
                          without parameters:<br><br>"
;
function 
callback_no_parameter()
{
  echo 
"<br><br><br>callback3() called:<br><br>";
}
var_dump( array_walk_recursive($input, 
                                
'callback_no_parameter'));

echo 
"<br><br><br>passing one more parameter 
                            to function with two parameters:<br><br>"
;
var_dump( array_walk_recursive($input, 
                                 
'callback_two_parameter', 
                                 
10));

?>

  11 EXERCISE   

<?php

/*
 * Passing different types of 
 * callback functions to 
 * array_walk_recursive()
 *   without parameters
 *   with less and more parameters
*/

echo "Testing array_walk_recursive() : 
                callback function variation."
;

$input = array(array('Apple', 'Banana'), 
                                      
'Mango', 
                                array(
'Orange'));

echo 
"<br><br><br>callback function with both parameters:<br><br>";
function 
callback_two_parameter($value, $key)
{
   
// dump the arguments to check
   // that they are passed
   // with proper type
   
var_dump($key);
   
// key
   
var_dump($value);
   
// value
   
echo "<br>";
   
// new line to separate the 
   // output between each element
}
var_dump( array_walk_recursive($input, 
                                     
'callback_two_parameter'));

echo 
"<br><br><br>callback function 
                            with only one parameter:<br><br>"
;
function 
callback_one_parameter($value)
{
   
// dump the arguments to check
   // that they are passed
   // with proper type
   
var_dump($value);
   
// value
   
echo "<br>";
   
// new line to separate the 
   // output between each element
}
var_dump( array_walk_recursive($input, 
                                       
'callback_one_parameter'));

echo 
"<br><br><br>callback function 
                            without parameters:<br><br>"
;
function 
callback_no_parameter()
{
  echo 
"callback3() called:<br>";
}
var_dump( array_walk_recursive($input, 
                                         
'callback_no_parameter'));

echo 
"<br><br><br>passing one more parameter to 
                          function with two parameters:<br><br>"
;
var_dump( array_walk_recursive($input, 
                                         
'callback_two_parameter', 10));

?>