1--TEST-- 2Test array_walk() function : usage variations - 'input' argument as diff. associative arrays 3--FILE-- 4<?php 5/* Prototype : bool array_walk(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() : '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( 1 => 25, 5 => 12, 0 => -80, -2 => 100, 5 => 30); 64echo "-- Associative array with numeric keys --\n"; 65var_dump( array_walk($input, "for_numeric", 10)); 66 67// String keys 68$input = array( "a" => "Apple", 'b' => 'Bananna', "c" => "carrot", 'o' => "Orange"); 69echo "-- Associative array with string keys --\n"; 70var_dump( array_walk($input, "for_string")); 71 72// binary keys 73$input = array( b"a" => "Apple", b"b" => "Banana"); 74echo "-- Associative array with binary keys --\n"; 75var_dump( array_walk($input, "for_string")); 76 77// Mixed keys - numeric/string 78$input = array( 0 => 1, 1 => 2, "a" => "Apple", "b" => "Banana", 2 =>3); 79echo "-- Associative array with numeric/string keys --\n"; 80var_dump( array_walk($input, "for_mixed")); 81 82echo "Done" 83?> 84--EXPECTF-- 85*** Testing array_walk() : 'input' as an associative array *** 86-- Associative array with numeric keys -- 87int(1) 88int(25) 89int(10) 90 91int(5) 92int(30) 93int(10) 94 95int(0) 96int(-80) 97int(10) 98 99int(-2) 100int(100) 101int(10) 102 103bool(true) 104-- Associative array with string keys -- 105string(1) "a" 106string(5) "Apple" 107 108string(1) "b" 109string(7) "Bananna" 110 111string(1) "c" 112string(6) "carrot" 113 114string(1) "o" 115string(6) "Orange" 116 117bool(true) 118-- Associative array with binary keys -- 119string(1) "a" 120string(5) "Apple" 121 122string(1) "b" 123string(6) "Banana" 124 125bool(true) 126-- Associative array with numeric/string keys -- 127int(0) 128int(1) 129 130int(1) 131int(2) 132 133string(1) "a" 134string(5) "Apple" 135 136string(1) "b" 137string(6) "Banana" 138 139int(2) 140int(3) 141 142bool(true) 143Done 144