Jump to content

Store array in session variable, then modify?


alejandro
 Share

Recommended Posts

Hello, after creating an array and assigning it to a variable in session:

	$order = array ();
	$order['token'] 		= 'token';
	$order['product']		= 'product-name';
	$session->order 		= $order;

I try to insert another item:

$session->order['price'] = 'price';

But it doesn't work: 

Notice: Indirect modification of overloaded property ProcessWire\Session::$order has no effect in...

Isn't possible to modify such session variable? I could build another array from the session variable, add the new item, and then store it again in session, but doesn't looks good.

Thanks!

Link to comment
Share on other sites

As you said, get the array from session in a temp var, insert your new item then assign back the array to the session var :

$order = array();
$order['token'] 		= 'token';
$order['product']		= 'product-name';
$session->order 		= $order;
bd($session->order);
$tmp = $session->order;
$tmp['price'] = 'price';
$session->order = $tmp;
bd($session->order);

 

Or use $session->setFor() :

$session->setFor('shop', 'token', 'token');
$session->setFor('shop', 'product', 'product-name');
bd($session->shop);
$session->setFor('shop', 'price', 'price');
bd($session->shop);
$session->setFor('shop', 'price', 'new-price');
bd($session->shop);

 

Edited by flydev
solution #2
  • Like 4
Link to comment
Share on other sites

Thank you both.

I like solution #3 ?

$session->order = array_merge ($session->order, ['price' => 50]);

didn't know about "setFor", didn't found in docs or cheatsheet. 

And, what's?

bd($session->shop);

The array_merge works without it

Link to comment
Share on other sites

18 minutes ago, alejandro said:

And, what's?


bd($session->shop);

I want to add to tpr's comment that bd() is actually one of the 2 most used features of tracy for me. bd() makes a dump to the tracy bar (also for ajax requests, that's great!) and d() dumps directly and is great when using the console (see my screenshot above)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...