1--TEST-- 2Directly modifying an unreferenced array when foreach'ing over it. 3--FILE-- 4<?php 5 6define('MAX_LOOPS',5); 7 8function withRefValue($elements, $transform) { 9 echo "\n---( Array with $elements element(s): )---\n"; 10 //Build array: 11 for ($i=0; $i<$elements; $i++) { 12 $a[] = "v.$i"; 13 } 14 $counter=0; 15 16 echo "--> State of array before loop:\n"; 17 var_dump($a); 18 19 echo "--> Do loop:\n"; 20 foreach ($a as $k=>$v) { 21 echo " iteration $counter: \$k=$k; \$v=$v\n"; 22 eval($transform); 23 $counter++; 24 if ($counter>MAX_LOOPS) { 25 echo " ** Stuck in a loop! **\n"; 26 break; 27 } 28 } 29 30 echo "--> State of array after loop:\n"; 31 var_dump($a); 32} 33 34 35echo "\nPopping elements off end of an unreferenced array"; 36$transform = 'array_pop($a);'; 37withRefValue(1, $transform); 38withRefValue(2, $transform); 39withRefValue(3, $transform); 40withRefValue(4, $transform); 41 42echo "\n\n\nShift elements off start of an unreferenced array"; 43$transform = 'array_shift($a);'; 44withRefValue(1, $transform); 45withRefValue(2, $transform); 46withRefValue(3, $transform); 47withRefValue(4, $transform); 48 49echo "\n\n\nRemove current element of an unreferenced array"; 50$transform = 'unset($a[$k]);'; 51withRefValue(1, $transform); 52withRefValue(2, $transform); 53withRefValue(3, $transform); 54withRefValue(4, $transform); 55 56echo "\n\n\nAdding elements to the end of an unreferenced array"; 57$transform = 'array_push($a, "new.$counter");'; 58withRefValue(1, $transform); 59withRefValue(2, $transform); 60withRefValue(3, $transform); 61withRefValue(4, $transform); 62 63echo "\n\n\nAdding elements to the start of an unreferenced array"; 64$transform = 'array_unshift($a, "new.$counter");'; 65withRefValue(1, $transform); 66withRefValue(2, $transform); 67withRefValue(3, $transform); 68withRefValue(4, $transform); 69 70?> 71--EXPECTF-- 72Popping elements off end of an unreferenced array 73---( Array with 1 element(s): )--- 74--> State of array before loop: 75array(1) { 76 [0]=> 77 string(3) "v.0" 78} 79--> Do loop: 80 iteration 0: $k=0; $v=v.0 81--> State of array after loop: 82array(0) { 83} 84 85---( Array with 2 element(s): )--- 86--> State of array before loop: 87array(2) { 88 [0]=> 89 string(3) "v.0" 90 [1]=> 91 string(3) "v.1" 92} 93--> Do loop: 94 iteration 0: $k=0; $v=v.0 95 iteration 1: $k=1; $v=v.1 96--> State of array after loop: 97array(0) { 98} 99 100---( Array with 3 element(s): )--- 101--> State of array before loop: 102array(3) { 103 [0]=> 104 string(3) "v.0" 105 [1]=> 106 string(3) "v.1" 107 [2]=> 108 string(3) "v.2" 109} 110--> Do loop: 111 iteration 0: $k=0; $v=v.0 112 iteration 1: $k=1; $v=v.1 113 iteration 2: $k=2; $v=v.2 114--> State of array after loop: 115array(0) { 116} 117 118---( Array with 4 element(s): )--- 119--> State of array before loop: 120array(4) { 121 [0]=> 122 string(3) "v.0" 123 [1]=> 124 string(3) "v.1" 125 [2]=> 126 string(3) "v.2" 127 [3]=> 128 string(3) "v.3" 129} 130--> Do loop: 131 iteration 0: $k=0; $v=v.0 132 iteration 1: $k=1; $v=v.1 133 iteration 2: $k=2; $v=v.2 134 iteration 3: $k=3; $v=v.3 135--> State of array after loop: 136array(0) { 137} 138 139 140 141Shift elements off start of an unreferenced array 142---( Array with 1 element(s): )--- 143--> State of array before loop: 144array(1) { 145 [0]=> 146 string(3) "v.0" 147} 148--> Do loop: 149 iteration 0: $k=0; $v=v.0 150--> State of array after loop: 151array(0) { 152} 153 154---( Array with 2 element(s): )--- 155--> State of array before loop: 156array(2) { 157 [0]=> 158 string(3) "v.0" 159 [1]=> 160 string(3) "v.1" 161} 162--> Do loop: 163 iteration 0: $k=0; $v=v.0 164 iteration 1: $k=1; $v=v.1 165--> State of array after loop: 166array(0) { 167} 168 169---( Array with 3 element(s): )--- 170--> State of array before loop: 171array(3) { 172 [0]=> 173 string(3) "v.0" 174 [1]=> 175 string(3) "v.1" 176 [2]=> 177 string(3) "v.2" 178} 179--> Do loop: 180 iteration 0: $k=0; $v=v.0 181 iteration 1: $k=1; $v=v.1 182 iteration 2: $k=2; $v=v.2 183--> State of array after loop: 184array(0) { 185} 186 187---( Array with 4 element(s): )--- 188--> State of array before loop: 189array(4) { 190 [0]=> 191 string(3) "v.0" 192 [1]=> 193 string(3) "v.1" 194 [2]=> 195 string(3) "v.2" 196 [3]=> 197 string(3) "v.3" 198} 199--> Do loop: 200 iteration 0: $k=0; $v=v.0 201 iteration 1: $k=1; $v=v.1 202 iteration 2: $k=2; $v=v.2 203 iteration 3: $k=3; $v=v.3 204--> State of array after loop: 205array(0) { 206} 207 208 209 210Remove current element of an unreferenced array 211---( Array with 1 element(s): )--- 212--> State of array before loop: 213array(1) { 214 [0]=> 215 string(3) "v.0" 216} 217--> Do loop: 218 iteration 0: $k=0; $v=v.0 219--> State of array after loop: 220array(0) { 221} 222 223---( Array with 2 element(s): )--- 224--> State of array before loop: 225array(2) { 226 [0]=> 227 string(3) "v.0" 228 [1]=> 229 string(3) "v.1" 230} 231--> Do loop: 232 iteration 0: $k=0; $v=v.0 233 iteration 1: $k=1; $v=v.1 234--> State of array after loop: 235array(0) { 236} 237 238---( Array with 3 element(s): )--- 239--> State of array before loop: 240array(3) { 241 [0]=> 242 string(3) "v.0" 243 [1]=> 244 string(3) "v.1" 245 [2]=> 246 string(3) "v.2" 247} 248--> Do loop: 249 iteration 0: $k=0; $v=v.0 250 iteration 1: $k=1; $v=v.1 251 iteration 2: $k=2; $v=v.2 252--> State of array after loop: 253array(0) { 254} 255 256---( Array with 4 element(s): )--- 257--> State of array before loop: 258array(4) { 259 [0]=> 260 string(3) "v.0" 261 [1]=> 262 string(3) "v.1" 263 [2]=> 264 string(3) "v.2" 265 [3]=> 266 string(3) "v.3" 267} 268--> Do loop: 269 iteration 0: $k=0; $v=v.0 270 iteration 1: $k=1; $v=v.1 271 iteration 2: $k=2; $v=v.2 272 iteration 3: $k=3; $v=v.3 273--> State of array after loop: 274array(0) { 275} 276 277 278 279Adding elements to the end of an unreferenced array 280---( Array with 1 element(s): )--- 281--> State of array before loop: 282array(1) { 283 [0]=> 284 string(3) "v.0" 285} 286--> Do loop: 287 iteration 0: $k=0; $v=v.0 288--> State of array after loop: 289array(2) { 290 [0]=> 291 string(3) "v.0" 292 [1]=> 293 string(5) "new.0" 294} 295 296---( Array with 2 element(s): )--- 297--> State of array before loop: 298array(2) { 299 [0]=> 300 string(3) "v.0" 301 [1]=> 302 string(3) "v.1" 303} 304--> Do loop: 305 iteration 0: $k=0; $v=v.0 306 iteration 1: $k=1; $v=v.1 307--> State of array after loop: 308array(4) { 309 [0]=> 310 string(3) "v.0" 311 [1]=> 312 string(3) "v.1" 313 [2]=> 314 string(5) "new.0" 315 [3]=> 316 string(5) "new.1" 317} 318 319---( Array with 3 element(s): )--- 320--> State of array before loop: 321array(3) { 322 [0]=> 323 string(3) "v.0" 324 [1]=> 325 string(3) "v.1" 326 [2]=> 327 string(3) "v.2" 328} 329--> Do loop: 330 iteration 0: $k=0; $v=v.0 331 iteration 1: $k=1; $v=v.1 332 iteration 2: $k=2; $v=v.2 333--> State of array after loop: 334array(6) { 335 [0]=> 336 string(3) "v.0" 337 [1]=> 338 string(3) "v.1" 339 [2]=> 340 string(3) "v.2" 341 [3]=> 342 string(5) "new.0" 343 [4]=> 344 string(5) "new.1" 345 [5]=> 346 string(5) "new.2" 347} 348 349---( Array with 4 element(s): )--- 350--> State of array before loop: 351array(4) { 352 [0]=> 353 string(3) "v.0" 354 [1]=> 355 string(3) "v.1" 356 [2]=> 357 string(3) "v.2" 358 [3]=> 359 string(3) "v.3" 360} 361--> Do loop: 362 iteration 0: $k=0; $v=v.0 363 iteration 1: $k=1; $v=v.1 364 iteration 2: $k=2; $v=v.2 365 iteration 3: $k=3; $v=v.3 366--> State of array after loop: 367array(8) { 368 [0]=> 369 string(3) "v.0" 370 [1]=> 371 string(3) "v.1" 372 [2]=> 373 string(3) "v.2" 374 [3]=> 375 string(3) "v.3" 376 [4]=> 377 string(5) "new.0" 378 [5]=> 379 string(5) "new.1" 380 [6]=> 381 string(5) "new.2" 382 [7]=> 383 string(5) "new.3" 384} 385 386 387 388Adding elements to the start of an unreferenced array 389---( Array with 1 element(s): )--- 390--> State of array before loop: 391array(1) { 392 [0]=> 393 string(3) "v.0" 394} 395--> Do loop: 396 iteration 0: $k=0; $v=v.0 397--> State of array after loop: 398array(2) { 399 [0]=> 400 string(5) "new.0" 401 [1]=> 402 string(3) "v.0" 403} 404 405---( Array with 2 element(s): )--- 406--> State of array before loop: 407array(2) { 408 [0]=> 409 string(3) "v.0" 410 [1]=> 411 string(3) "v.1" 412} 413--> Do loop: 414 iteration 0: $k=0; $v=v.0 415 iteration 1: $k=1; $v=v.1 416--> State of array after loop: 417array(4) { 418 [0]=> 419 string(5) "new.1" 420 [1]=> 421 string(5) "new.0" 422 [2]=> 423 string(3) "v.0" 424 [3]=> 425 string(3) "v.1" 426} 427 428---( Array with 3 element(s): )--- 429--> State of array before loop: 430array(3) { 431 [0]=> 432 string(3) "v.0" 433 [1]=> 434 string(3) "v.1" 435 [2]=> 436 string(3) "v.2" 437} 438--> Do loop: 439 iteration 0: $k=0; $v=v.0 440 iteration 1: $k=1; $v=v.1 441 iteration 2: $k=2; $v=v.2 442--> State of array after loop: 443array(6) { 444 [0]=> 445 string(5) "new.2" 446 [1]=> 447 string(5) "new.1" 448 [2]=> 449 string(5) "new.0" 450 [3]=> 451 string(3) "v.0" 452 [4]=> 453 string(3) "v.1" 454 [5]=> 455 string(3) "v.2" 456} 457 458---( Array with 4 element(s): )--- 459--> State of array before loop: 460array(4) { 461 [0]=> 462 string(3) "v.0" 463 [1]=> 464 string(3) "v.1" 465 [2]=> 466 string(3) "v.2" 467 [3]=> 468 string(3) "v.3" 469} 470--> Do loop: 471 iteration 0: $k=0; $v=v.0 472 iteration 1: $k=1; $v=v.1 473 iteration 2: $k=2; $v=v.2 474 iteration 3: $k=3; $v=v.3 475--> State of array after loop: 476array(8) { 477 [0]=> 478 string(5) "new.3" 479 [1]=> 480 string(5) "new.2" 481 [2]=> 482 string(5) "new.1" 483 [3]=> 484 string(5) "new.0" 485 [4]=> 486 string(3) "v.0" 487 [5]=> 488 string(3) "v.1" 489 [6]=> 490 string(3) "v.2" 491 [7]=> 492 string(3) "v.3" 493} 494