Solved

php array shift associative array

Posted on 2009-05-10
6
827 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
Comment Utility
Is this what you need:
$sizeArray = array_merge(array('deleted'=>0),$sizeArray);

Open in new window

0
 
LVL 39

Expert Comment

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

Open in new window

0
 

Author Comment

by:jackjohnson44
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mail Not Sent 6 41
echo button 13 45
Storing files securely - database or filesystem 3 85
windows 10 bash shell 4 32
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now