Solved

PHP push array using array_push() to same array

Posted on 2014-02-02
7
500 Views
Last Modified: 2014-02-02
Hi,

I have this bit of code:

$fields = array(
    //set for all selections
    'METHOD' => $METHOD,
    'VERSION' => $VERSION,
    'USER' => $USER,
    'PWD' => $PWD,
    'SIGNATURE' => $SIGNATURE,
    'BUTTONCODE' => $BUTTONCODE,
    'BUTTONSUBTYPE' => $BUTTONSUBTYPE,
    'L_BUTTONVAR1' => $L_BUTTONVAR1,
    'OPTION0NAME' => $OPTION0NAME
);


$aFieldsAppend = array (
    //set in 3, 6, 12,
    'L_BUTTONVAR2' => $L_BUTTONVAR2,
    'BUTTONTYPE' => $BUTTONTYPE,
    'L_OPTION0SELECT0' => $L_OPTION0SELECT0,
    'OPTION0TYPE' => $OPTION0TYPE
);
array_push($fields, $aFieldsAppend);

Open in new window


I'm trying to use the array_push() and push another array with key=>value to the same array but when I use it the second array $aFeildsAppend it's pushed to $fields[0] rather than just $fields.

I tried this code too but it doesn't work:

array_push($fields, ('L_BUTTONVAR2' => $L_BUTTONVAR2,
    'BUTTONTYPE' => $BUTTONTYPE,
    'L_OPTION0SELECT0' => $L_OPTION0SELECT0,
    'OPTION0TYPE' => $OPTION0TYPE));

Open in new window


How can I push the second array to the first one properly so it's all on the same array?

Thanks,
Victor
0
Comment
Question by:Victor Kimura
[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
  • 3
  • 3
7 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 333 total points
ID: 39828201
So you are trying to merge two arrays on the same index?
0
 
LVL 10

Assisted Solution

by:acbxyz
acbxyz earned 167 total points
ID: 39828275
Either use array_merge() as Cathal said or just set the keys in your destination array
$fields = array(
    //set for all selections
    'METHOD' => $METHOD,
    'VERSION' => $VERSION,
    'USER' => $USER,
    'PWD' => $PWD,
    'SIGNATURE' => $SIGNATURE,
    'BUTTONCODE' => $BUTTONCODE,
    'BUTTONSUBTYPE' => $BUTTONSUBTYPE,
    'L_BUTTONVAR1' => $L_BUTTONVAR1,
    'OPTION0NAME' => $OPTION0NAME
);

$fields['L_BUTTONVAR2'] = $L_BUTTONVAR2;
$fields['BUTTONTYPE'] = $BUTTONTYPE;
$fields['L_OPTION0SELECT0'] = $L_OPTION0SELECT0;
$fields['OPTION0TYPE'] = $OPTION0TYPE;

Open in new window

0
 

Author Comment

by:Victor Kimura
ID: 39828306
@Cathal Yes, that's right.

Thanks @acbxyz. I didn't think of that solution. Simple.

But I found that I could do this too:

$fields = array_merge($fields, $aFieldsAppend);

Open in new window

0
Independent Software Vendors: 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!

 

Author Comment

by:Victor Kimura
ID: 39828503
I've requested that this question be closed as follows:

Accepted answer: 500 points for acbxyz's comment #a39828275
Assisted answer: 0 points for vkimura2007's comment #a39828306

for the following reason:

found a solution using array_merge() too.  =)
0
 
LVL 58

Expert Comment

by:Gary
ID: 39828309
Ermm did you miss the link in my comment.
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 333 total points
ID: 39828504
Method to use and was used was given here http:/#a39828201
0
 

Author Comment

by:Victor Kimura
ID: 39828760
@Cathal, Oh, so sorry! I did miss it! Sorry. =( Can I award half the points to you still? Will try. Sorry, Cathal.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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