如何将项目添加到多维数组中?基本上,我正在制作一个应用程序,计算人们在超市购买的物品和数量。
Sue购买了2罐黄油和1支牙膏
John购买了1个桃子和1根香蕉。
我认为这个数组看起来像这样:
$sue[butter] = array();
$sue[butter][] = 2;
$sue[toothpaste] = array();
$sue[toothpaste][] = 1;
$john[peach] = array();
$john[peach][] = 1;
$john[banana] = array();
$john[banana][] = 1;
我的当前代码只能记录商品和商品数量。
public $items = array();
public function AddItem($product_id)
{
if (array_key_exists($product_id , $this->items))
{
$this->items[$product_id] = $this ->items[$product_id] + 1;
} else {
$this->items[$product_id] = 1;
}
}
我不知道如何将这个放进每个人的数组中。
谢谢!