so I just started using a new php and none of my functions are working in the same proper way. I have a shopping cart function that checks for items in a session array and then it places the item in the array. If the item already it exitis it updates that input by the qty entered.
$arr=$_SESSION['item'];
function searchForId($id, $array) {
if (is_array($array)) {
foreach ($array as $key => $val) {
if ($val['sku'] === $id) {
return $key;
}
}
return 'no';
}
}
$theSearch = searchForId($sku, $arr);
if(!isset($_SESSION['item']))
{
$newitem = array ('sku' => $sku , 'qty' => $qty);
$_SESSION['item'][1] = $newitem;
}
else
{
if ($theSearch != 'no') {
$nqty= $_SESSION['item'][$theSearch]['qty'] + $qty;
$_SESSION['item'][$theSearch]['qty'] = $nqty;
}
else
{
if(count($_SESSION['item']) >=1)
{
$newitem = array ('sku' => $sku , 'qty' => $qty);
$_SESSION['item'][] = $newitem;
}
else
{
$newitem = array ('sku' => $sku , 'qty' => $qty);
$_SESSION['item'][1] = $newitem;
}
}
}
The error im getting is that the line $nqty= $_SESSION['item'][$theSearch]['qty'] + $qty;
and the error thrown is
Fatal error: Cannot use string offset as an array in
Can someone please help!