Php关联数组值插入,显示错误的键
这是我的问题。我有一个关联数组,希望键与传递到函数中的item对象的item id匹配。如果数组中不存在item id键,我想将item id作为键添加到购物车数组中,并将新数组Php关联数组值插入,显示错误的键,php,arrays,associative,Php,Arrays,Associative,这是我的问题。我有一个关联数组,希望键与传递到函数中的item对象的item id匹配。如果数组中不存在item id键,我想将item id作为键添加到购物车数组中,并将新数组[“item”=>$item,“Quantity=>1]作为键值 如果密钥已经存在,我只想更新存储在数组中的数量,该数量将通过使用项目id索引购物车数组来检索 以下是我认为会产生这些结果的代码(位于Cart.class.php中): 但是,当使用var\u dump($this->cart\u items)时,会输出以下
[“item”=>$item,“Quantity=>1]
作为键值
如果密钥已经存在,我只想更新存储在数组中的数量,该数量将通过使用项目id索引购物车数组来检索
以下是我认为会产生这些结果的代码(位于Cart.class.php中):
但是,当使用var\u dump($this->cart\u 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\u items[$item->ItemID]=array()
。
我的问题是我做错了什么,为什么没有将ID用作数组中的键
谢谢您尝试过(string)$item->ItemID吗?如果我没有弄错的话,关联数组键必须是字符串。否则,您使用的是数组索引,而不是键。代码对我来说很好
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);
提及
进行var_转储($item)从add_to_cart函数内部是的,但我希望该ItemID也成为cart_items关联数组的索引。我的意思是$item上的var_dump并在此处发布结果。我已经测试了此代码,它在v7中运行良好。我同意@Terminus,该项不能是您所期望的……它可能是您的项的ID为1吗?传递的参数为to
add_to_cart()
函数不是数组;正如OP所说,它是一个“传递的项目对象”。仔细阅读说明!在传递到函数之前,我正在将数组转换为对象。请检查行$item=(object)$itemrarray;
@GergelyLukacsy
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);