I'm trying to count total number of sales of a particular product and its variant and sizes.
Current Code :
$arr2 = array();
$counter = 0;
for($arr = 0; $arr < count($product_id); $arr++){
if (in_array($product_id[$arr], $arr2)) {
++$counter;
continue;
}
else{
$arr2[] = $product_id[$arr];
}
}
echo 'number of duplicates: '.$counter;
print_r($arr2);
Result of above code
number of duplicates: 85
Array (
[0] => Array ( [id] => 1089 [variant] => Crunchy Classic [size] => 1 Kg )
[1] => Array ( [id] => 1089 [variant] => Creamy Dark Chocolate [size] => 1 Kg )
[2] => Array ( [id] => 1090 [variant] => Universal [size] => Universal )
[3] => Array ( [id] => 1089 [variant] => Creamy Dark Chocolate [size] => 340 gm )
[4] => Array ( [id] => 1089 [variant] => Crunchy Classic [size] => 340 gm )
)
I want the result to be like
Array (
[0] => Array ( [id] => 1089 [variant] => Crunchy Classic [size] => 1 Kg [count] => 2)
[1] => Array ( [id] => 1089 [variant] => Creamy Dark Chocolate [size] => 1 Kg [count] => 1)
[2] => Array ( [id] => 1090 [variant] => Crunchy Classic [size] => 340gm [count] => 2)
[3] => Array ( [id] => 1089 [variant] => Creamy Dark Chocolate [size] => 340 gm [count] => 1)
[4] => Array ( [id] => 1089 [variant] => Crunchy Classic [size] => 340 gm [count] => 2)
)
To simplify more, My current data is this
ProductID | SKU | Variant | size. |
-------------------------------------
1089 | 123 | A | 1KG |
1089 | 123 | B | 250gm |
1089 | 123 | B | 1KG |
1089 | 123 | A | 1KG |
1089 | 123 | C | 250gm |
1090 | 124 | C | 250gm |
1090 | 124 | C | 1KG |
1090 | 124 | C | 1KG |
1090 | 124 | C | 1KG |
-------------------------------------
So the result should be
ID | SKU | Variant | Size | Count |
1089 | 123 | A | 1KG | 2 |
1089 | 123 | B | 1KG | 1 |
1089 | 123 | B | 250gm | 1 |
1089 | 123 | C | 250gm | 2 |
1089 | 123 | C | 1KG | 3 |
Records are in mutiple of variant and sizes and i need to count different variant and sizes count
Model
ProductID, SKU, Price, Variant, Size, TransactionID, Status
here is the basic query :
DB::table('orders')->whereBetween('created_at', [$date['start'], $date['end']])->get();
My Answer:
To achieve the desired result, you can modify your code as follows:
php
$arr2 = array();
foreach($product_id as $product){
$found = false;
foreach($arr2 as &$item){
if($item['id'] == $product['id'] && $item['variant'] == $product['variant'] && $item['size'] == $product['size']){
$item['count']++;
$found = true;
break;
}
}
if(!$found){
$arr2[] = array('id' => $product['id'], 'variant' => $product['variant'], 'size' => $product['size'], 'count' => 1);
}
}
print_r($arr2);
This code will iterate over each product in the `$product_id` array and check if it already exists in the `$arr2` array. If it does, it will increment the count for that product. If it doesn't exist, it will add a new entry with a count of 1.
This will give you the desired result with the count of different variants and sizes for each product.
Rate this post
5 of 5 based on 1574 votesComments