Solved

PHP push array using array_push() to same array

Posted on 2014-02-02
7
474 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
  • 3
  • 3
7 Comments
 
LVL 58

Accepted Solution

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

Assisted Solution

by:acbxyz
acbxyz earned 167 total points
Comment Utility
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
Comment Utility
@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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Victor Kimura
Comment Utility
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
Comment Utility
Ermm did you miss the link in my comment.
0
 
LVL 58

Assisted Solution

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

Author Comment

by:Victor Kimura
Comment Utility
@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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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.
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 …

743 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

15 Experts available now in Live!

Get 1:1 Help Now