New / modification of the syntax of square brackets
An existing array can be changed by expanding the value.
This is implemented by assigning a key name to the argument in square brackets. You can also omit the key name, in which case the variable name plus a pair of square brackets ("[]").
$ arr [key] = value;
$ Arr [] = Value;
// Key can be in Integer or String
// Value can be any value. in case
$ ARR does not exist, will create a new one. This is also a replacement method for defining an array. To change a value, just give it a new value. If you want to delete a key name / value, you should use it
unset ().
php $ arr = array (5 => 1, 12 => 2); $ arr [] = 56; // this is the same as $ arr [= 56; // at this point of the script $ Arr ["x"] = 42; // this adds a new element to // the array with key "x" unset ($ arr [5]); // this Removes the element from the arrayunset ($ ARR); / / THIS DELETES The Whole Array?>
Note: As mentioned above, if you give an square bracket but do not specify a key name, take the current maximum integer index value, the new key name will be the value 1. If there is currently no integer index, the key name will be 0. This value will be overwritten if the established key name is already worth it.
WARNING Since PHP 4.3.0, the above index generation method changes. Now if you add a new value to an array of the current maximum key name is a negative value, the newly generated index will be zero (0). Previously newly generated indexes were added to the current maximum index, and the index of the positive value was the same.
Note that the maximum integer key name used here is not necessarily in the array. It will only exist after the last array regenerates an index. The following example illustrates: