Solved

PHP: Prepend key and item to array

Posted on 2011-09-20
1
331 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 21

Accepted Solution

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

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

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now