PHP关联数组值插入,显示错误的键名

4
这是我的问题。我有一个关联数组,希望键与传入函数的项对象的项ID匹配。如果项ID键在数组中不存在,我希望将项ID添加到购物车数组作为键,并将一个新数组["Item"=>$item,"Quantity=>1]作为键值添加进去。
如果键已经存在,我只想更新通过索引项ID检索出来的存储在数组中的数量。
以下是我认为可以产生这些结果的代码(位于Cart.class.php中):
  private $cart_items = array();

  public function add_to_cart($item, $quantity = 1){
    if(!isset($item) || !isset($item->ItemID)){
        throw new Exception("Error adding item to cart");
    }
    if(!array_key_exists($item->ItemID,$this->cart_items)){
        $this->cart_items[$item->ItemID] = array("Item"=>$item,"Quantity"=>$quantity);
    }else{
        $this->cart_items[$item->ItemID]["Quantity"] += $quantity;
    }
    $this->number_of_cart_items+=$quantity;
}

然而,当使用var_dump($this->cart_items)时,会输出以下内容:
    array(2){
   [
      0
   ]   => NULL   [
      1
   ]   => array(2)   {
      [
         "Item"
      ]      => object(stdClass)#2 (8)      {
         [
            "ItemID"
         ]         => int(11)         [
            "ItemName"
         ]         => string(18) "Kids check T-Shirt"         [
            "ShortDescription"
         ]         => string(20) "A kids check T-Shirt"         [
            "LongDescription"
         ]         => string(51) " A kids check T-shirt perfect for formal occasions!"         [
            "ItemPrice"
         ]         => float(33.59)         [
            "ImagePath"
         ]         => string(51) "kozzi-26129586-1591x2387.jpg"         [
            "QuantityAvailable"
         ]         => int(100)         [
            "ItemSupplier_SupplierID"
         ]         => int(1)
      }      [
         "Quantity"
      ]      => int(1)
   }
}

我的问题是$item->ItemID没有被用作关联数组的键(你可以看到键是[0] [1],第一个是null,尽管我使用了$this->cart_items[$item->ItemID] = array()。 我的问题是我做错了什么,为什么id没有被用作数组中的键? 谢谢。

在 add_to_cart 函数内部执行 var_dump($item)。 - user5051310
是的,但我希望ItemID也成为cart_items关联数组的索引。 - Eladian
1
我是指在$item上使用var_dump,并将结果发布在这里。 - user5051310
你的物品ID是1吗? - Jonathan Chow
你解决了这个问题吗? - user5051310
显示剩余4条评论
2个回答

0

这段代码在我的机器上运行良好。

class Cart{

    private $cart_items = array();
    private $number_of_cart_items;

    public function add_to_cart($item, $quantity = 1){
        if(!isset($item) || !isset($item->ItemID)){
            throw new Exception("Error adding item to cart");
        }
        if(!array_key_exists($item->ItemID,$this->cart_items)){
            $this->cart_items[$item->ItemID] = array("Item"=>$item,"Quantity"=>$quantity);
        }else{
            $this->cart_items[$item->ItemID]["Quantity"] += $quantity;
        }
        $this->number_of_cart_items+=$quantity;
        return $this->cart_items;
    }
}


$itemArray = array(
    "ItemID" => 11,
    "ItemName" => "Kids check T-Shirt",
    "ShortDescription" => "A kids check T-Shirt",
    "LongDescription"=>"A kids check T-shirt perfect for formal occasions!",
    "ItemPrice" => 33.59,
    "ImagePath" => "kozzi-26129586-1591x2387.jpg",
    "QuantityAvailable" => 100,
    "ItemSupplier_SupplierID" => 1
);
$quantity = 1;
$item  = (object)$itemArray ;


$cart = new Cart();
$add_to_cart = $cart->add_to_cart($item,$quantity);


print_r($add_to_cart);    
var_dump($add_to_cart);

参考https://3v4l.org/N96Vc

传递给 add_to_cart() 函数的参数不是一个数组;正如 OP 所说,它是一个“传递的项目对象”。仔细阅读描述! - Gergely Lukacsy
1
在将数组传递到函数之前,我正在将其转换为对象。请检查代码行 $item = (object)$itemArray ;。@GergelyLukacsy - zakhefron

-3
你尝试过(string)$item->ItemID吗?如果我没记错,关联数组的键必须是字符串。否则,你使用的是数组索引,而不是键。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接