1--TEST-- 2Test array_unshift() function : usage variations - assoc. array with diff. keys for 'array' argument 3--FILE-- 4<?php 5/* 6 * Testing the functionality of array_unshift() by passing different 7 * associative arrays having different possible keys to $array argument. 8 * The $var argument passed is a fixed value 9*/ 10 11echo "*** Testing array_unshift() : associative array with different keys ***\n"; 12 13//get an unset variable 14$unset_var = 10; 15unset ($unset_var); 16 17//get a resource variable 18$fp = fopen(__FILE__, "r"); 19 20//get a class 21class classA 22{ 23 public function __toString(){ 24 return "Class A object"; 25 } 26} 27 28// get a heredoc string 29$heredoc = <<<EOT 30Hello world 31EOT; 32 33// initializing $var argument 34$var = 10; 35 36// different variations of associative arrays to be passed to $array argument 37$arrays = array ( 38 39 // empty array 40/*1*/ array(), 41 42 // arrays with integer keys 43 array(0 => "0"), 44 array(1 => "1"), 45 array(1 => "1", 2 => "2", 3 => "3", 4 => "4"), 46 47 // arrays with string keys 48/*7*/ array('\tHello' => 111, 're\td' => "color", 49 '\v\fworld' => 2.2, 'pen\n' => 33), 50 array("\tHello" => 111, "re\td" => "color", 51 "\v\fworld" => 2.2, "pen\n" => 33), 52 array("hello", $heredoc => "string"), // heredoc 53 54 // array with object, unset variable and resource variable 55 array(@$unset_var => "hello", $fp => 'resource'), 56 57 // array with mixed keys 58/*11*/ array('hello' => 1, "fruit" => 2.2, 59 $fp => 'resource', 133 => "int", 60 @$unset_var => "unset", $heredoc => "heredoc") 61); 62 63// loop through the various elements of $arrays to test array_unshift() 64$iterator = 1; 65foreach($arrays as $array) { 66 echo "-- Iteration $iterator --\n"; 67 68 /* with default argument */ 69 // returns element count in the resulting array after arguments are pushed to 70 // beginning of the given array 71 $temp_array = $array; 72 var_dump( array_unshift($temp_array, $var) ); 73 74 // dump the resulting array 75 var_dump($temp_array); 76 77 /* with optional arguments */ 78 // returns element count in the resulting array after arguments are pushed to 79 // beginning of the given array 80 $temp_array = $array; 81 var_dump( array_unshift($temp_array, $var, "hello", 'world') ); 82 83 // dump the resulting array 84 var_dump($temp_array); 85 $iterator++; 86} 87 88echo "Done"; 89?> 90--EXPECTF-- 91*** Testing array_unshift() : associative array with different keys *** 92 93Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d 94 95Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d 96-- Iteration 1 -- 97int(1) 98array(1) { 99 [0]=> 100 int(10) 101} 102int(3) 103array(3) { 104 [0]=> 105 int(10) 106 [1]=> 107 string(5) "hello" 108 [2]=> 109 string(5) "world" 110} 111-- Iteration 2 -- 112int(2) 113array(2) { 114 [0]=> 115 int(10) 116 [1]=> 117 string(1) "0" 118} 119int(4) 120array(4) { 121 [0]=> 122 int(10) 123 [1]=> 124 string(5) "hello" 125 [2]=> 126 string(5) "world" 127 [3]=> 128 string(1) "0" 129} 130-- Iteration 3 -- 131int(2) 132array(2) { 133 [0]=> 134 int(10) 135 [1]=> 136 string(1) "1" 137} 138int(4) 139array(4) { 140 [0]=> 141 int(10) 142 [1]=> 143 string(5) "hello" 144 [2]=> 145 string(5) "world" 146 [3]=> 147 string(1) "1" 148} 149-- Iteration 4 -- 150int(5) 151array(5) { 152 [0]=> 153 int(10) 154 [1]=> 155 string(1) "1" 156 [2]=> 157 string(1) "2" 158 [3]=> 159 string(1) "3" 160 [4]=> 161 string(1) "4" 162} 163int(7) 164array(7) { 165 [0]=> 166 int(10) 167 [1]=> 168 string(5) "hello" 169 [2]=> 170 string(5) "world" 171 [3]=> 172 string(1) "1" 173 [4]=> 174 string(1) "2" 175 [5]=> 176 string(1) "3" 177 [6]=> 178 string(1) "4" 179} 180-- Iteration 5 -- 181int(5) 182array(5) { 183 [0]=> 184 int(10) 185 ["\tHello"]=> 186 int(111) 187 ["re\td"]=> 188 string(5) "color" 189 ["\v\fworld"]=> 190 float(2.2) 191 ["pen\n"]=> 192 int(33) 193} 194int(7) 195array(7) { 196 [0]=> 197 int(10) 198 [1]=> 199 string(5) "hello" 200 [2]=> 201 string(5) "world" 202 ["\tHello"]=> 203 int(111) 204 ["re\td"]=> 205 string(5) "color" 206 ["\v\fworld"]=> 207 float(2.2) 208 ["pen\n"]=> 209 int(33) 210} 211-- Iteration 6 -- 212int(5) 213array(5) { 214 [0]=> 215 int(10) 216 [" Hello"]=> 217 int(111) 218 ["re d"]=> 219 string(5) "color" 220 ["world"]=> 221 float(2.2) 222 ["pen 223"]=> 224 int(33) 225} 226int(7) 227array(7) { 228 [0]=> 229 int(10) 230 [1]=> 231 string(5) "hello" 232 [2]=> 233 string(5) "world" 234 [" Hello"]=> 235 int(111) 236 ["re d"]=> 237 string(5) "color" 238 ["world"]=> 239 float(2.2) 240 ["pen 241"]=> 242 int(33) 243} 244-- Iteration 7 -- 245int(3) 246array(3) { 247 [0]=> 248 int(10) 249 [1]=> 250 string(5) "hello" 251 ["Hello world"]=> 252 string(6) "string" 253} 254int(5) 255array(5) { 256 [0]=> 257 int(10) 258 [1]=> 259 string(5) "hello" 260 [2]=> 261 string(5) "world" 262 [3]=> 263 string(5) "hello" 264 ["Hello world"]=> 265 string(6) "string" 266} 267-- Iteration 8 -- 268int(3) 269array(3) { 270 [0]=> 271 int(10) 272 [""]=> 273 string(5) "hello" 274 [1]=> 275 string(8) "resource" 276} 277int(5) 278array(5) { 279 [0]=> 280 int(10) 281 [1]=> 282 string(5) "hello" 283 [2]=> 284 string(5) "world" 285 [""]=> 286 string(5) "hello" 287 [3]=> 288 string(8) "resource" 289} 290-- Iteration 9 -- 291int(7) 292array(7) { 293 [0]=> 294 int(10) 295 ["hello"]=> 296 int(1) 297 ["fruit"]=> 298 float(2.2) 299 [1]=> 300 string(8) "resource" 301 [2]=> 302 string(3) "int" 303 [""]=> 304 string(5) "unset" 305 ["Hello world"]=> 306 string(7) "heredoc" 307} 308int(9) 309array(9) { 310 [0]=> 311 int(10) 312 [1]=> 313 string(5) "hello" 314 [2]=> 315 string(5) "world" 316 ["hello"]=> 317 int(1) 318 ["fruit"]=> 319 float(2.2) 320 [3]=> 321 string(8) "resource" 322 [4]=> 323 string(3) "int" 324 [""]=> 325 string(5) "unset" 326 ["Hello world"]=> 327 string(7) "heredoc" 328} 329Done 330