Under what circumstances would
$array[$index] = $element;
and
unset($array[$index]);
$array[$index] = $element;
be different?
Assuming I am not using any references in my array, are these logically equivalent?
The order is changed if you first remove a key and then add it again:
$arr = array("foo1" => "bar1", "foo2" => "bar2");
$arr["foo1"] = "baz";
print_r($arr);
$arr = array("foo1" => "bar1", "foo2" => "bar2");
unset($arr["foo1"]);
$arr["foo1"] = "baz";
print_r($arr);
Output:
Array
(
[foo1] => baz
[foo2] => bar2
)
Array
(
[foo2] => bar2
[foo1] => baz
)