Solved

PHP push array using array_push() to same array

Posted on 2014-02-02
7
485 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
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
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.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
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…
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 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…

810 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