Am building a shopping cart and would like to append new items to a wishlist by adding them to a session but they shouldnt replace the existing items
This is what i have tried but returns an error of
[] operator not supported for strings
This is the code:
public function addItem($value, (int)$id){
if(isset($_SESSION[$value])){
$_SESSION[$value][] = array();
array_push($_SESSION[$value],array(
'id'=>$id
));
return true;
}
}
The values of $value is a string
I have also followed on This yii link and also on This link but still am getting the same error
By doing it this way
public function addItem($value, $id){
if(isset($_SESSION[$value])){
$_SESSION[$value] = array();
array_push($_SESSION[$value],array(
'id'=>$id
));
return true;
}
}
Adds the items but replaces whatever i had previously
WHAT DO I NEED TO CHANGE FOR IT TO WORK
$_SESSION['items'][] = Array($id,$value);$_SESSION[$value][] = 'new item';to push new items into the array, the initialization with$_SESSION[$value] = array()is unnecessary.