Skylifter notes on 20-Jan-2004 that the [] empty bracket notation does not return the array count as array_push does. There's another difference between array_push and the recommended empty bracket notation.

Empy bracket doesn't check if a variable is an array first as array_push does. If array_push finds that a variable isn't an array it prints a Warning message if E_ALL error reporting is on.

So array_push is safer than [], until further this is changed by the PHP developers.

If you push an array onto the stack, PHP will add the whole array to the next element instead of adding the keys and values to the array. If this is not what you want, you're better off using array_merge() or traverse the array you're pushing on and add each element with $stack[$key] = $value.

CONTEXT...drewdeal: this turns out to be better and easier than array_push()patelbhadresh: great!... so u discover new idea...drewdeal: because you can't do: $emp_list_bic = array_push($emp_list, c=>"ANY CLIENT");drewdeal: array_push returns a count and affects current array.. and does not support set keys!drewdeal: yeah. My one-liner makes a new array as a derivative of the prior array

I can only assume that PHP sorts the array as elements are added to make it easier for it to find a specified element by its key later. In many cases it won't matter if the array is not stored internally in the same order you added the elements, but if, for instance, you execute a foreach on the array later, the elements may not be processed in the order you need them to be.

If you want to add elements to the END of an associative array you should use the unary array union operator (+=) instead...

Unfortunately array_push returns the new number of items in the arrayIt does not give you the key of the item you just added, in numeric arrays you could do -1, you do however need to be sure that no associative key exists as that would break the assumption

It would have been better if array_push would have returned the key of the item just added like the below function(perhaps a native variant would be a good idea...)

Where $key is a unique identifier and $value is the value to be stored. Since the $key works off a string or number, if you already have a $key with the same value as an existing $key, the element will be overwritten.

regarding the speed of oneill's solution to insert a value into a non-associative array, I've done some tests and I found that it behaves well if you have a small array and more insertions, but for a huge array and a little insersions I sugest using this function: