2

Im trying to put multiple posts into one session. Heres the line of code that i use to do it.

$_SESSION['answers'][] = array_push($_SESSION['answers'], $_POST);

As i do so, the following array comes out once i try to add the second array to the session:

Array
(
    [0] => Array
        (
            [checkbox] => Optie 2
            [category] => Dieren en natuur
            [subcategory] => Wilde dieren
            [vraagid] => 116
            [type] => 3afbeeldingen
            [media] => image
            [submit] =>  
        )

        [1] => Array
        (
            [checkbox] => Optie 1
            [category] => Dieren en natuur
            [subcategory] => Wilde dieren
            [vraagid] => 117
            [type] => 3afbeeldingen
            [media] => image
            [submit] =>  
        )

    [2] => 2
 )

I am talking about the last array,

[2] => 2.

This is automattically added. Now when i try to get another array in to the session, it gives me the same issue, it adds the correct array, with checkbox and other vars, but also makes a

[4] => 4

Is this an issue with the array_push function? Because the array that i push is correct, i already checked that.

1
  • array_push returns an integer indicating the number of new elements in the array Commented Jan 9, 2014 at 14:03

3 Answers 3

6

Either add the value to your array:

$_SESSION['answers'][] = $_POST;

or use array_push

array_push($_SESSION['answers'], $_POST);

You try to do to much at once :)

Sign up to request clarification or add additional context in comments.

Comments

1

Won't it work, when you just do:

array_push($_SESSION['answers'], $_POST);

or

$_SESSION['answers'][] = $_POST;

Comments

1

array_push() is basically the same as $array[] = .... array_push() returns the new number of elements in the array, so basically you are adding the new element to your array and then you are adding the amount of elements in the array to the array again (hence the 2).

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.