1--TEST-- 2Test array_walk_recursive() function : usage variations - 'input' argument as diff. associative arrays 3--FILE-- 4<?php 5/* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata]) 6 * Description: Apply a user function to every member of an array 7 * Source code: ext/standard/array.c 8*/ 9 10/* 11 * Passing 'input' argument as an associative array 12 * with Numeric & string keys 13*/ 14 15echo "*** Testing array_walk_recursive() : 'input' as an associative array ***\n"; 16 17// callback functions 18/* Prototype : for_numeric( int $value, int $key, int $user_data) 19 * Parameters : $value - value from key/value pair of the array 20 * $key - key from key/value pair of the array 21 * $user_data - data to be added to 'value' 22 * Description : Function adds values with keys & user_data 23 */ 24function for_numeric($value, $key, $user_data) 25{ 26 // dump the input values to see if they are 27 // passed with correct type 28 var_dump($key); 29 var_dump($value); 30 var_dump($user_data); 31 echo "\n"; // new line to separate the output between each element 32} 33 34/* Prototype : for_string( string $value, string $key) 35 * Parameters : $value - values in given input array 36 * $key - keys in given input array 37 * Description : Function appends key to the value 38 */ 39function for_string($value, $key) 40{ 41 // dump the input values to see if they are 42 // passed with correct type 43 var_dump($key); 44 var_dump($value); 45 echo "\n"; // new line to separate the output between each element 46} 47 48/* Prototype : for_mixed( mixed $value, mixed $key) 49 * Parameters : $value - values in given input array 50 * $key - keys in given input array 51 * Description : Function displays each element of an array with keys 52 */ 53function for_mixed($value, $key) 54{ 55 // dump the input values to see if they are 56 // passed with correct type 57 var_dump($key); 58 var_dump($value); 59 echo "\n"; // new line to separate the output between each element 60} 61 62// Numeric keys 63$input = array( 0 => array(1 => 25, 5 => 12, 0 => -80), 1 => array(-2 => 100, 5 => 30)); 64echo "-- Associative array with numeric keys --\n"; 65var_dump( array_walk_recursive($input, "for_numeric", 10)); 66 67// String keys 68$input = array( "a" => "Apple", 'z' => array('b' => 'Bananna', "c" => "carrot"), 'x' => array('o' => "Orange")); 69echo "-- Associative array with string keys --\n"; 70var_dump( array_walk_recursive($input, "for_string")); 71 72// binary key 73$input = array( b"a" => "Apple", b"b" => "Banana"); 74echo "-- Associative array with binary keys --\n"; 75var_dump( array_walk_recursive($input, "for_string")); 76 77// Mixed keys - numeric/string 78$input = array( 0 => array(0 => 1, 1 => 2), "x" => array("a" => "Apple", "b" => "Banana"), 2 =>3); 79echo "-- Associative array with numeric/string keys --\n"; 80var_dump( array_walk_recursive($input, "for_mixed")); 81 82echo "Done" 83?> 84--EXPECTF-- 85*** Testing array_walk_recursive() : 'input' as an associative array *** 86-- Associative array with numeric keys -- 87int(1) 88int(25) 89int(10) 90 91int(5) 92int(12) 93int(10) 94 95int(0) 96int(-80) 97int(10) 98 99int(-2) 100int(100) 101int(10) 102 103int(5) 104int(30) 105int(10) 106 107bool(true) 108-- Associative array with string keys -- 109string(1) "a" 110string(5) "Apple" 111 112string(1) "b" 113string(7) "Bananna" 114 115string(1) "c" 116string(6) "carrot" 117 118string(1) "o" 119string(6) "Orange" 120 121bool(true) 122-- Associative array with binary keys -- 123string(1) "a" 124string(5) "Apple" 125 126string(1) "b" 127string(6) "Banana" 128 129bool(true) 130-- Associative array with numeric/string keys -- 131int(0) 132int(1) 133 134int(1) 135int(2) 136 137string(1) "a" 138string(5) "Apple" 139 140string(1) "b" 141string(6) "Banana" 142 143int(2) 144int(3) 145 146bool(true) 147Done 148