2

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?

3
  • Are you getting any sort of error message? Commented Jan 26, 2016 at 15:18
  • nope, there is no error message. Commented Jan 26, 2016 at 15:20
  • @A-2-A I did not yet, i had no time left yesterday, but i will in the next few hours, don't worry, i will accept one as right answer if it works :) Commented Jan 27, 2016 at 9:33

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

This is the working solution, this does exactly what i need. Thank you. Little Edit to your code: You are missing a ")" at the end before the semi-colon.
i had to edit the code a bit more as i thought first, but your answer brought me on the right track, this is how it works now: array_push($_SESSION['wishList']->contents[$pid] = array('qty' => '1.0000'));
0
$_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']

Comments

0
$_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.

1 Comment

An elaboration to your answer would definitely help others to understand better than a single command-line.

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.