I’m trying to create a PHP Session Wrapper Class and to do it – in order to make it flexible I need to be able to dynamically create session associative array like so:
'basket' => array(
27 => array(
'colour' => 'blue',
'qty' => 2,
'price' => 20.50
)
)
The way to set the session would be by calling:
Session::set(array('basket', $item_id, 'colour'), 'blue');
Session::set(array('basket', $item_id, 'qty'), 2);
Session::set(array('basket', $item_id, 'price'), 20.50);
Now – I’m really not sure how to create session associative array out of the first parameter of the set() method.
It might be just one item or more than 4 presented here – the idea is to make it dynamic – regardless of number of items in the first array parameter.
Any idea how could this be achieved?
I think I understand what you are saying. The following code is messy, but it is a proof of concept. The method you are suggesting allows you to reset a deeper value without resetting the entire array, which I believe this accomplishes (up to four keys in array). You’ll need to do some validation to make sure that the array indices are actually set to prevent any errors, but it will work fine after that.