housamz
housamz

Reputation: 465

PHP saving multidimensional array in session

<?php
session_start();

$_SESSION[] = array('itemName'=> "'".$_POST["name"]."'",
                    'itemPrice'=> "'".$_POST["price"]."'"
                    );
print_r($_SESSION);
?>

I am posting the data through jQuery, and although the print_r displays correct data, but PHP above doesn't save in session, any idea?

Upvotes: 1

Views: 4433

Answers (2)

lopisan
lopisan

Reputation: 7830

You have to use $_SESSION['name'] to store to session, not just $_SESSION[]

<?php
session_start();

$_SESSION['name'] = array('itemName'=> "'".$_POST["name"]."'",
                    'itemPrice'=> "'".$_POST["price"]."'"
                    );
print_r($_SESSION);
?>

Upvotes: 2

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

You will have to give some kind of index/key (name) to the session-variable, so PHP know how to refer to it.

Here's how $_SESSION['test'] is assigned:

$_SESSION['test'] = array('itemName'=> "'".$_POST["name"]."'",
                        'itemPrice'=> "'".$_POST["price"]."'"
                        );

Upvotes: 2

Related Questions