How to add elements to a multidimensional array? I basically make an application that calculates what people buy at the supermarket and how much it is.
Sue buys 2 baths with oil and 1. Toothpaste
John buys 1 peach and 1 banana.
I think the array will look something like this.
$sue[butter] = array();
$sue[butter][] = 2;
$sue[toothpaste] = array();
$sue[toothpaste][] = 1;
$john[peach] = array();
$john[peach][] = 1;
$john[banana] = array();
$john[banana][] = 1;
My current code can only write an element and the number of elements.
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;
}
}
I just don't know how to put this inside an array for each person.
Thanks!
source
share