Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 927
  • Last Modified:

php array shift associative array

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
jackjohnson44
Asked:
jackjohnson44
  • 4
2 Solutions
 
Roger BaklundCommented:
Is this what you need:
$sizeArray = array_merge(array('deleted'=>0),$sizeArray);

Open in new window

0
 
Roger BaklundCommented:
Sorry, that should be:
$sizeArray = array_merge(array('Delete'=>'DELETE'),$sizeArray);

Open in new window

0
 
jackjohnson44Author Commented:
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
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!

 
Roger BaklundCommented:
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
 
ycTINCommented:
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
 
Roger BaklundCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now