Solved

php array shift associative array

Posted on 2009-05-10
6
901 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

623 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