Search code examples
phparrayssessionarray-push

php Post arrays in Session array


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.


Solution

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