Solved

php array shift associative array

Posted on 2009-05-10
6
876 Views
Last Modified: 2012-05-06
I have an associative array and pass it to a function to build a select pulldown.

my values are $sizeArray[small] = 1, 2 for large, 3 for extra large, .....

I want to add a key value pair on to the front
        $selectOption["Delete"] = "DELETE";
        array_unshift($sizeArray, "DELETE");

This is actually adding a new item to the front with an index of 0, and its value is an array.  How do I push a key value pair on the beginning.
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24350673
Is this what you need:
$sizeArray = array_merge(array('deleted'=>0),$sizeArray);

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24350681
Sorry, that should be:
$sizeArray = array_merge(array('Delete'=>'DELETE'),$sizeArray);

Open in new window

0
 

Author Comment

by:jackjohnson44
ID: 24350854
Thanks a lot, that worked.

I am actually looking for something slightly different.
I was hoping to build an array using something like this
$newoptions["delete"] = "delete"

I want to use this method because I might want to add multiple options, and they might come from a database.  Make sense?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 200 total points
ID: 24350881
I'm not sure if I understand. You could do this to prepend the $sizeArray with $newoptions, which could contain multiple items:

$newoptions["delete"] = "delete";
$sizeArray = array_merge($newoptions,$sizeArray);

If the members of the array comes from the database or not does not matter.
0
 
LVL 7

Accepted Solution

by:
ycTIN earned 300 total points
ID: 24350984
If  just want to append new item to array front or rear , use the + operator is most simple

<?php
$sizeArray['small'] = 1;
$sizeArray['large'] = 2;
$sizeArray['extra'] = 3;
 
$optArray['edit'] = 'EDIT';
$optArray['delete'] = 'DETELE';
 
$newArray = $optArray + $sizeArray;
var_dump($newArray);
?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24352584
As long as the involved arrays are associative arrays (string keys) the + operator works fine, but be aware that this can not be used with list type arrays (numeric keys). The + operator returns the UNION of the arrays.

http://php.net/manual/en/language.operators.array.php
$a1=array('a','b');
$a2=array('c','d','e');
$a3 = $a1 + $a2;
var_dump($a3);  # array(3) { [0]=>  string(1) "a" [1]=>  string(1) "b" [2]=>  string(1) "e" } 

Open in new window

0

Featured Post

Technology Partners: 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

730 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