无法在对象中增加PHP数组变量

无法在对象中增加PHP数组变量,php,arrays,shopping-cart,Php,Arrays,Shopping Cart,我正在向我的购物车添加以下内容: function addItem($id, $qty="1"){ if (($this->isInCart($id)) == false){ $this->cart[] = array( 'id' => $id, 'qty' => $qty); } else{ $this->cart[$id]['qty']++; } } 如果商品已经在我的购物车中,我只告诉该方法将当前$

我正在向我的购物车添加以下内容:

function addItem($id, $qty="1"){
    if (($this->isInCart($id))  == false){ 
        $this->cart[] = array( 'id' => $id, 'qty' => $qty);
    } else{
        $this->cart[$id]['qty']++;
    }
}
如果商品已经在我的购物车中,我只告诉该方法将当前$id增加1,但这似乎不适用于这些调用:

$basket->addItem('monkey','200');
$basket->addItem('dog', '10');
$basket->addItem('dog');
在第二次添加狗项时,以下功能仅报告我的篮子中的10只狗:

function numberOfProduct($id){
    unset($number);
    foreach($this->cart as $n ){
        if ($n['id'] == $id){           
            $number = $number + $n['qty'];
        }
    }
    return $number;
}
我确信问题在于我在addToBasket方法中增加数组,但是当我在过程编码中使用完全相同的方法时,它工作得很好

我真的被卡住了

编辑:按要求在购物车方法中

function isInCart($id){
    $inCart=false;
    $itemsInCart=count($this->cart);
    if ($itemsInCart > 0){
        foreach($this->cart as $cart){
            if ($cart['id']==$id){
                return $inCart=true;
                break;
            }
        }
    }   
    return $inCart;
}

将其添加到数组时,使用的是数字键,而不是ID值:

$this->cart[] = array( 'id' => $id, 'qty' => $qty);
将其更改为:

$this->cart[$id] = array( 'id' => $id, 'qty' => $qty);

将此更改合并到您的
isInCart()
方法中,您应该会做得很好。

将其添加到数组中时,您使用的是数字键,而不是ID值:

$this->cart[] = array( 'id' => $id, 'qty' => $qty);
function addItem($id, $qty="1"){
...
    $this->cart[$id]['qty']++;
...
将其更改为:

$this->cart[$id] = array( 'id' => $id, 'qty' => $qty);
将此更改合并到您的
isInCart()
方法中,您应该会做得很好

function addItem($id, $qty="1"){
...
    $this->cart[$id]['qty']++;
...
将函数的第二个参数设置为字符串。再次调用函数时,您传入了一个字符串

$basket->addItem('monkey','200');
$basket->addItem('dog', '10');
$basket->addItem('dog');
如果我有一些字符串
$string=“123”
,我尝试用
$string++
来增加它,我不会增加它的数值。从数字中删除引号,它应该会按预期工作

function addItem($id, $qty=1){
if (($this->isInCart($id))  == false){ 
    $this->cart[] = array( 'id' => $id, 'qty' => $qty);
} else{
    $this->cart[$id]['qty']++;
}
}
并调用如下函数

$basket->addItem('monkey',200);
$basket->addItem('dog', 10);
$basket->addItem('dog');
如果你需要一个数字,最好只用一个数字。如果
$qty
来自用户输入,我可以理解使用字符串,但如果是这种情况,则需要使用
$qty=intval($qty)
来获取它的数字版本

将函数的第二个参数设置为字符串。再次调用函数时,您传入了一个字符串

$basket->addItem('monkey','200');
$basket->addItem('dog', '10');
$basket->addItem('dog');
如果我有一些字符串
$string=“123”
,我尝试用
$string++
来增加它,我不会增加它的数值。从数字中删除引号,它应该会按预期工作

function addItem($id, $qty=1){
if (($this->isInCart($id))  == false){ 
    $this->cart[] = array( 'id' => $id, 'qty' => $qty);
} else{
    $this->cart[$id]['qty']++;
}
}
并调用如下函数

$basket->addItem('monkey',200);
$basket->addItem('dog', 10);
$basket->addItem('dog');

如果你需要一个数字,最好只用一个数字。如果
$qty
来自用户输入,我可以理解使用字符串,但如果是这种情况,您需要使用
$qty=intval($qty)
来获取它的数字版本应该是
$this->cart[$id]['qty']+=$qty
你能给我们展示一下
isInCart
方法吗?@JosephSilber为什么
+=
,而不是
+
?我正在学习PHP,我想知道什么时候不使用
++
@Kamil-因为使用
++
意味着当你在购物车中添加一些已经存在的东西时,你忽略了你的输入参数
$qty
。如果有人将10只猴子添加到他们的购物车中,然后再添加10只猴子,请逐步完成您的代码。会发生什么?@nickb我知道。我认为在对象中的关联数组上使用
++
在PHP或类似的东西中可能不起作用应该是
$this->cart[$id]['qty']+=$qty
你能给我们展示一下
isInCart
方法吗?@JosephSilber为什么
+=
,而不是
+
?我正在学习PHP,我想知道什么时候不使用
++
@Kamil-因为使用
++
意味着当你在购物车中添加一些已经存在的东西时,你忽略了你的输入参数
$qty
。如果有人将10只猴子添加到他们的购物车中,然后再添加10只猴子,请逐步完成您的代码。会发生什么?@nickb我知道。我认为在对象中的关联数组上使用
++
在PHP或类似的东西中可能不起作用。谢谢。我不得不对我的foreach循环做一些其他的更改,但是我完全忘记了我是按数字而不是id索引的事实!谢谢你。我不得不对我的foreach循环做一些其他的更改,但是我完全忘记了我是按数字而不是id索引的事实!