dynamically inserting value to array with index

bunkdeath picture bunkdeath · Nov 1, 2011 · Viewed 13.7k times · Source

Pushing values to an array caused the index to start with 0 if the index were any other values except starting from 0.

$a=array("a"=>"Dog","b"=>"Cat");
array_push($a,"Horse","Bird");

this will insert Horse and Bird with index 0 and 1.

Can I insert those values with different index? like

speed => Horse
fly => Bird

Answer

ThiefMaster picture ThiefMaster · Nov 1, 2011

Yes, by not using array_push at all:

$a['speed'] = 'Horse';
$a['fly'] = 'Bird';