Return to array_map_variation4.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test array_map() function : usage variations - associative array with different keys 3: --FILE-- 4: <?php 5: /* Prototype : array array_map ( callback $callback , array $arr1 [, array $... ] ) 6: * Description: Applies the callback to the elements of the given arrays 7: * Source code: ext/standard/array.c 8: */ 9: 10: /* 11: * Test array_map() by passing associative array with different keys for $arr1 argument 12: */ 13: 14: echo "*** Testing array_map() : associative array with diff. keys for 'arr1' argument ***\n"; 15: 16: function callback($a) 17: { 18: return ($a); 19: } 20: 21: // get an unset variable 22: $unset_var = 10; 23: unset ($unset_var); 24: 25: // get a resource variable 26: $fp = fopen(__FILE__, "r"); 27: 28: // get a class 29: class classA{ 30: public function __toString(){ 31: return "Class A object"; 32: } 33: } 34: 35: // get a heredoc string 36: $heredoc = <<<EOT 37: Hello world 38: EOT; 39: 40: // initializing the array 41: $arrays = array ( 42: 43: // empty array 44: /*1*/ array(), 45: 46: // arrays with integer keys 47: /*2*/ array(0 => "0"), 48: array(1 => "1"), 49: array(1 => "1", 2 => "2", 3 => "3", 4 => "4"), 50: 51: // arrays with float keys 52: /*5*/ array(2.3333 => "float"), 53: array(1.2 => "f1", 3.33 => "f2", 4.89999922839999 => "f3", 33333333.333333 => "f4"), 54: 55: // arrays with string keys 56: array('\tHello' => 111, 're\td' => 'color', '\v\fworld' => 2.2, 'pen\n' => 33), 57: /*8*/ array("\tHello" => 111, "re\td" => "color", "\v\fworld" => 2.2, "pen\n" => 33), 58: array("hello", $heredoc => "string"), // heredoc 59: 60: // array with object, unset variable and resource variable 61: array(new classA() => 11, @$unset_var => "hello", $fp => 'resource'), 62: 63: // array with mixed values 64: /*11*/ array('hello' => 1, new classA() => 2, "fruit" => 2.2, 65: $fp => 'resource', 133 => "int", 444.432 => "float", 66: @$unset_var => "unset", $heredoc => "heredoc") 67: ); 68: 69: // loop through the various elements of $arrays to test array_map() 70: $iterator = 1; 71: foreach($arrays as $arr1) { 72: echo "-- Iteration $iterator --\n"; 73: var_dump( array_map('callback', $arr1) ); 74: $iterator++; 75: } 76: 77: echo "Done"; 78: ?> 79: --EXPECTF-- 80: *** Testing array_map() : associative array with diff. keys for 'arr1' argument *** 81: 82: Warning: Illegal offset type in %s on line %d%d 83: 84: Warning: Illegal offset type in %s on line %d%d 85: 86: Warning: Illegal offset type in %s on line %d%d 87: 88: Warning: Illegal offset type in %s on line %d%d 89: -- Iteration 1 -- 90: array(0) { 91: } 92: -- Iteration 2 -- 93: array(1) { 94: [0]=> 95: string(1) "0" 96: } 97: -- Iteration 3 -- 98: array(1) { 99: [1]=> 100: string(1) "1" 101: } 102: -- Iteration 4 -- 103: array(4) { 104: [1]=> 105: string(1) "1" 106: [2]=> 107: string(1) "2" 108: [3]=> 109: string(1) "3" 110: [4]=> 111: string(1) "4" 112: } 113: -- Iteration 5 -- 114: array(1) { 115: [2]=> 116: string(5) "float" 117: } 118: -- Iteration 6 -- 119: array(4) { 120: [1]=> 121: string(2) "f1" 122: [3]=> 123: string(2) "f2" 124: [4]=> 125: string(2) "f3" 126: [33333333]=> 127: string(2) "f4" 128: } 129: -- Iteration 7 -- 130: array(4) { 131: ["\tHello"]=> 132: int(111) 133: ["re\td"]=> 134: string(5) "color" 135: ["\v\fworld"]=> 136: float(2.2) 137: ["pen\n"]=> 138: int(33) 139: } 140: -- Iteration 8 -- 141: array(4) { 142: [" Hello"]=> 143: int(111) 144: ["re d"]=> 145: string(5) "color" 146: ["world"]=> 147: float(2.2) 148: ["pen 149: "]=> 150: int(33) 151: } 152: -- Iteration 9 -- 153: array(2) { 154: [0]=> 155: string(5) "hello" 156: ["Hello world"]=> 157: string(6) "string" 158: } 159: -- Iteration 10 -- 160: array(1) { 161: [""]=> 162: string(5) "hello" 163: } 164: -- Iteration 11 -- 165: array(6) { 166: ["hello"]=> 167: int(1) 168: ["fruit"]=> 169: float(2.2) 170: [133]=> 171: string(3) "int" 172: [444]=> 173: string(5) "float" 174: [""]=> 175: string(5) "unset" 176: ["Hello world"]=> 177: string(7) "heredoc" 178: } 179: Done