PHP: Add key and value to beginning of array

Using PHP, how can I add a key and value to the beginning of an array?
<pre><?php

$zzz=array();

$zzz['b']='Banana';
$zzz['c']='Cherry';
$zzz['d']='Durian';

$zzz = array_merge($zzz,'a'->'Apple');

print_r($zzz);

?>
</pre>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Kim WalkerConnect With a Mentor Web Programmer/TechnicianCommented:
If you want "a" to be first in the array order, simply reverse the order of the array_merge arguments:

$zzz = array_merge(array('a'=>'Apple'),$zzz);

Open in new window

0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Try:

$zzz = array_merge($zzz,array('a'->'Apple'));

Open in new window

0
 
haloexpertsexchangeCommented:
0
 
hankknightAuthor Commented:
xmediaman, your idea gives a parse error.

halo, your idea does not preserve the key.

Please provide a working example based on the code I posted, thanks!
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Correction:

$zzz = array_merge($zzz,array('a'=>'Apple') );

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.