Php升级抛出数组和foreach()的错误

时间:2015-04-21 19:55:59

标签: php isapi-extension

所以我刚刚开始使用一个新的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;并且抛出的错误是

  

致命错误:无法在

中将字符串偏移用作数组

有人可以帮忙!

0 个答案:

没有答案