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