0

所以我刚开始使用一个新的 php 并且我的所有函数都没有以相同的正确方式工作。我有一个购物车函数,它检查会话数组中的项目,然后将项目放入数组中。如果该项目已经存在,它会按输入的数量更新该输入。

$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;   
}
}



}

我得到的错误是该行 $nqty= $_SESSION['item'][$theSearch]['qty'] + $qty;和抛出的错误是

致命错误:不能将字符串偏移量用作数组

有人可以帮忙吗!

4

0 回答 0