?
Solved

PHP: Prepend key and item to array

Posted on 2011-09-20
1
Medium Priority
?
344 Views
Last Modified: 2012-05-12
How can I add an item with a key to the begginning of an array?

The problem with my code is that it adds an array with a key of 0 to the beginning.  I want this:

Array
(
    [a] => Apple
    [b] => Banana
    [c] => Cherry
    [d] => Durian
)

Open in new window

<?php

$zzz=array();

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

$a = array();
$a['a']='Apple';

array_unshift($zzz, $a);

print_r($zzz);



?>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 22

Accepted Solution

by:
Kim Walker earned 2000 total points
ID: 36568086
How about:

$zzz = array_merge($a,$zzz);

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question