Solved

php array shift associative array

Posted on 2009-05-10
6
856 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 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