Last active
January 3, 2016 04:39
-
-
Save parkan/8410301 to your computer and use it in GitHub Desktop.
PHP's sequential/associative array behavior appears to depend on whether the array has a continuous, monotonically increasing sequence of indices starting at 0. Unsetting an index in the middle of the sequence will make the array behave associatively. Unsetting out of order indices such that order is restored (e.g. [0,2,1] to [0,1]) will restore…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php > $foo = []; | |
php > $foo[0] = 'zero'; | |
php > $foo[1] = 'first'; | |
php > $foo[0] = 'second'; | |
php > var_dump($foo); | |
array(2) { | |
[0] => | |
string(6) "second" | |
[1] => | |
string(5) "first" | |
} | |
php > | |
php > $foo = []; | |
php > $foo[0] = 'zero'; | |
php > $foo[2] = 'first'; | |
php > $foo[1] = 'second'; | |
php > var_dump($foo); | |
array(3) { | |
[0] => | |
string(4) "zero" | |
[2] => | |
string(5) "first" | |
[1] => | |
string(6) "second" | |
} | |
php > unset($foo[2]); | |
php > $foo[2] = 'first'; | |
php > var_dump($foo); | |
array(3) { | |
[0] => | |
string(4) "zero" | |
[1] => | |
string(6) "second" | |
[2] => | |
string(5) "first" | |
} | |
php > $foo = [ 'a', 'b', 'c', 'd' ]; | |
php > var_dump($foo); | |
php > $foo[4] = 'e'; | |
php > var_dump($foo); | |
array(5) { | |
[0] => | |
string(1) "a" | |
[1] => | |
string(1) "b" | |
[2] => | |
string(1) "c" | |
[3] => | |
string(1) "d" | |
[4] => | |
string(1) "e" | |
} | |
php > unset($foo[1]); | |
php > var_dump($foo); | |
array(4) { | |
[0] => | |
string(1) "a" | |
[2] => | |
string(1) "c" | |
[3] => | |
string(1) "d" | |
[4] => | |
string(1) "e" | |
} | |
php > $foo[1] = 'Z'; | |
php > var_dump($foo); | |
array(5) { | |
[0] => | |
string(1) "a" | |
[2] => | |
string(1) "c" | |
[3] => | |
string(1) "d" | |
[4] => | |
string(1) "e" | |
[1] => | |
string(1) "Z" | |
} | |
php > unset($foo[1]); | |
php > array_splice($foo, 1, 0, 'y'); | |
php > var_dump($foo); | |
array(5) { | |
[0] => | |
string(1) "a" | |
[1] => | |
string(1) "y" | |
[2] => | |
string(1) "c" | |
[3] => | |
string(1) "d" | |
[4] => | |
string(1) "e" | |
} | |
php > $foo[5] = 'x'; | |
php > var_dump($foo); | |
array(6) { | |
[0] => | |
string(1) "a" | |
[1] => | |
string(1) "y" | |
[2] => | |
string(1) "c" | |
[3] => | |
string(1) "d" | |
[4] => | |
string(1) "e" | |
[5] => | |
string(1) "x" | |
} | |
php > $foo[3] = 'u'; | |
php > var_dump($foo); | |
array(6) { | |
[0] => | |
string(1) "a" | |
[1] => | |
string(1) "y" | |
[2] => | |
string(1) "c" | |
[3] => | |
string(1) "u" | |
[4] => | |
string(1) "e" | |
[5] => | |
string(1) "x" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment