php Post arrays in Session array

1.4k Views Asked by At

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.

3

There are 3 best solutions below

0
On BEST ANSWER

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 :)

0
On

Won't it work, when you just do:

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

or

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

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).