Thursday, September 20, 2012

How do I add a value to a PHP array without a key?

$myarray = array(
 'key1' => 'value',
 'another value'
 )

OK, so that works, but sometimes you just want to use bracket notation to add 'another value' entry without disturbing the array or including a key.
$myarray[]='another another value';

This adds a (hidden) key to the value. If you aren't using numbers in your index, it is likely that the hidden key might be zero. If not, it may be max integer key +1. You can't necessarily rely on the key number being consistent, but at least it's how to add a stand-alone value to an array without including a key.

Blog Archive