I want to add multiple values into arrays, for same key.
I have this code:
public function getOnlySellers()
{
$sellers[] = array();
foreach ($this->getProducts() as $product) {
$product_id = $product['product_id'];
$getseller = $this->getSellerbyProduct($product_id);
$sellers[$getseller] = $product_id;
}
return $sellers;
}
Here I have for example: seller 1 with values: 100 and 101
Seller 2 with values: 107
But on my code, on seller 1 for example, is showing only last value 101, but not both.
What is wrong ?
And the call code:
$call = $this->cart->getOnlySellers();
foreach ($call as $seller => $products)
{
$show = "$show <br> $seller has value $products";
}
Thanks!
My Answer:
>products as $product) {_ if ($product['seller']) {_ $sellers[$product['seller']][] = $product;_ }_ }_ return $sellers;_}_This code will create an array where the keys are seller names and the values are arrays of products associated with that seller. If a seller has multiple products, they will all be added to the same key in the array.
Rate this post
5 of 5 based on 6635 votesComments