Solved

PHP push array using array_push() to same array

Posted on 2014-02-02
7
497 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

738 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