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_dumparray_walk_recursive($fruits'test_print'));

echo 
"<br><br><br>With all parameters:<br><br>";
var_dumparray_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(10, -10), array(023, -041), 
                                            array(
0x5A0X1F, -0x6E)),

       
// float value
       
array(array(3.40.8, -2.9), array(6.25e28.20E-3)),

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

       
// bool values
/*4*/  
array( array(truefalse), array(TRUEFALSE)),

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

       
// empty array
       
array(),

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

       
// mixed array
/*8*/  
array(168.345, array("Fruits"), 
                                   array(
truenull), 
                                   array(
FALSE), 
                                   array(-
980.005'banana'))
);

for(
$count 0$count count($input_values); $count++) {
  echo 
"<br><br>Iteration: ".($count 1)."<br>";
  
var_dumparray_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(
123), array(1))
);

var_dumparray_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(&
$value30), 
                                        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_dumparray_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( => array(=> 25=> 12=> -80), 
                                         
=> array(-=> 100=> 30));
echo 
"<br><br><br>Associative array with numeric keys:<br><br>";
var_dumparray_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_dumparray_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_dumparray_walk_recursive($input"for_string"));

// Mixed keys - numeric/string
$input = array( => array(=> 1=> 2), 
                       
"x" => array("a" => "Apple""b" => "Banana"), 
                        
=>3);
echo 
"<br><br><br>Associative array with numeric/string keys:<br><br>";
var_dumparray_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(25), array(100));

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

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

echo 
"<br><br><br>With three arguments:<br><br>";
var_dumparray_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_dumparray_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_dumparray_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_dumparray_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_dumparray_walk_recursive($input
                                
'callback_no_parameter'));

echo 
"<br><br><br>passing one more parameter 
                            to function with two parameters:<br><br>"
;
var_dumparray_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_dumparray_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_dumparray_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_dumparray_walk_recursive($input
                                         
'callback_no_parameter'));

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

?>