7

I'm building a 3 page submission form, and I'd quite like all of the $_POST results to be stored in a single session variable.

So page 1 starts by setting up the array and adding the first lot of post data:

$_SESSION['results'] = array();
$_SESSION['results'] = $_POST // first lot of post data

This works great and returns an array like:

Array
(
  [name] => bob
  [address] => 1 foobar way
  [age] => 100
)

So when I get the resuts from page 2, I want to simply append them to the existing array without invoking a new array+key

array_push($_SESSION['results'], $_POST); //second lot of post data

To get something like this:

Array
(
  [name] => bob
  [address] => 1 foobar way
  [age] => 100
  [job] => rubbish php dev
  [salary] => 1000
)

But instead I get:

Array
(
  [name] => bob
  [address] => 1 foobar way
  [age] => 100
  [0] => Array
    (
      [job] => rubbish php dev
      [salary] => 1000
    )
)

Even more annoying is that I'm sure I had this working properly before I tweaked the code. What am I doing wrong?

1
  • 1
    you are searching for array_merge Commented Mar 20, 2013 at 13:58

4 Answers 4

8

You can also use the + operator:

$combined = $_SESSION['results'] + $_POST;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. Any idea if that has less overhead than using the array_merge function?
@user2189903 I would certainly guess so, but you would have to benchmark to be sure.
4

array_merge() is the function you're after.

1 Comment

What a fraggle rock I am! I had array_merge but changed it to array_push. Thanks for the quick responses guys.
1

array_merge() is your answer see http://php.net/manual/en/function.array-merge.php

Comments

0

You have to use array_merge(), look at this: array_merge()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.