Solved

PHP: Prepend key and item to array

Posted on 2011-09-20
1
336 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 500 total points
ID: 36568086
How about:

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

Open in new window

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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