Marcel Wasilewski
Marcel Wasilewski

Reputation: 2679

Adding array to existing $_SESSION array

I got following existing array in my $_SESSION:

[wishList] => wishList Object
(
[contents] => Array
(
[109] => Array
(
[qty] => 1.0000
)

[89] => Array
(
[qty] => 1.0000
)

[62] => Array
(
[qty] => 1.0000
)

Now i am trying to add a new object to it like this:

echo $_SESSION['wishList']->contents = array('60' => array('qty' => '1.0000'));

So the array would look like this:

[wishList] => wishList Object
    (
    [contents] => Array
    (
    [109] => Array
    (
    [qty] => 1.0000
    )

    [89] => Array
    (
    [qty] => 1.0000
    )

    [62] => Array
    (
    [qty] => 1.0000
    )

    [60] => Array
    (
    [qty] => 1.0000
    )

It is not working the way i try it. Where is my fault?

Upvotes: 2

Views: 65

Answers (3)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

Tr this:-

array_push($_SESSION['wishList']->contents,array('60' => array('qty' => '1.0000')));

Or try this :-

array_push($_SESSION['wishList']->contents[$pid] = array('qty' => '1.0000')); // where $pid = 60;

Upvotes: 1

SOFe
SOFe

Reputation: 8224

$_SESSION['wishList']->contents[60] = array('qty' => '1.0000');

This changes the array $_SESSION['wishList']->contents by setting its value at index 60 to the array.

Upvotes: 0

ameenulla0007
ameenulla0007

Reputation: 2683

$_SESSION['wishList']->wishList['contents'] = "your new value string or array";

you missed out selecting main object.

i tried a piece of code. which works for me.

session_start();
class newObject {
    var $aProp  = array("type1"=>"test1", "type2"=>"test2");
    function __contruct() {
        return array("type1"=>"test1", "type2"=>"test2");
    }
}
$newObject  = new newObject;
$_SESSION["newObj"] = $newObject;
print_r($_SESSION);
// prints Array ( [newObj] => newObject Object ( [aProp] => Array ( [type1] => test1 [type2] => test2 ) ) ) 
echo "<br>";
$_SESSION["newObj"]->aProp  = array("type3"=>"test3", "type4"=>"test4");
// prints Array ( [newObj] => newObject Object ( [aProp] => Array ( [type3] => test3 [type4] => test4 ) ) )
print_r($_SESSION);

according to me.. what you have missed is pointing out the correct target. here in your case it is $_SESSION['wishList']->wishList['contents']

Upvotes: 0

Related Questions