Here I am trying to fetch the income of a seller, but the problem is that I am unable to group the duplicate the array value into one.
Logic Function.
function getMonthlyIncomeOfSellerById($sellerId)
{
$data = array();
$ddd = array();
$query = "SELECT date_format(created_at,'%M'), product_id,sell_discount,SUM(sell_quantity) FROM sellers_sells WHERE seller_id=? AND YEAR(created_at) = YEAR(CURRENT_DATE()) GROUP BY date_format(created_at,'%M'),seller_id,product_id ORDER BY created_at";
$stmt = $this->con->prepare($query);
$stmt->bind_param('s',$sellerId);
$stmt->execute();
$stmt->bind_result($monthName,$productId,$sellDiscount,$sellQuantity);
while ($stmt->fetch())
{
$d['monthName'] = $monthName;
$d['productId'] = $productId;
$d['sellDiscount'] = $sellDiscount;
$d['sellQuantity'] = $sellQuantity;
array_push($data, $d);
}
$stmt->close();
$netProfit = 0;
$maxProfit = 0;
foreach ($data as $dt)
{
$product = $this->getProductById($dt['productId']);
$product['productPrice'] = ($product['productPrice']/100)*$dt['sellDiscount'];
if ($product['productName']==='FAROOQUI MASSAGE OIL' && $product['productBrand']==='FHC' && $product['productSize']==='50ML')
{
$maxPrice = $product['productPrice']*$dt['sellQuantity'];
$price = ($product['productPrice']-10)*$dt['sellQuantity'];
}
else if ($product['productName']==='FAROOQUI MASSAGE OIL' && $product['productBrand']==='FHC' && $product['productSize']==='100ML')
{
$maxPrice = $product['productPrice']*$dt['sellQuantity'];
$price = ($product['productPrice']-15)*$dt['sellQuantity'];
}
else if ($product['productName']==='FAROOQUI MASSAGE OIL' && $product['productBrand']==='FHC' && $product['productSize']==='200ML')
{
$maxPrice = $product['productPrice']*$dt['sellQuantity'];
$price = ($product['productPrice']-30)*$dt['sellQuantity'];
}
$netProfit = $netProfit+$price;
$maxProfit = $maxProfit+$maxPrice;
$dts['monthName'] = $dt['monthName'];
$dts['netProfit'] = $netProfit;
$dts['maxProfit'] = $maxProfit;
array_push($ddd, $dts);
}
print_r($ddd);
}
Getting the out put with duplicate value.
Array
(
[0] => Array
(
[monthName] => January
[netProfit] => 2050
[maxProfit] => 2800
)
[1] => Array
(
[monthName] => March
[netProfit] => 2214
[maxProfit] => 3024
)
[2] => Array
(
[monthName] => March
[netProfit] => 4149
[maxProfit] => 5604
)
[3] => Array
(
[monthName] => March
[netProfit] => 4523
[maxProfit] => 6148
)
)
But here I want to group it with the month, and add all the values into one.
Like this.
Array
(
[0] => Array
(
[monthName] => January
[netProfit] => 2050
[maxProfit] => 2800
)
[1] => Array
(
[monthName] => March
[netProfit] => 10886
[maxProfit] => 14776
)
)
How can I do this?
Thanks
You could group by 'month' by usign the monthName
as key. If the key doesn't exists, create a new entry with default values. Then, you could add (+
) the current value from the data.
$monthName = $dt['monthName'];
if (!isset($ddd[$monthName])) {
$ddd[$monthName] = [
'monthName' => $monthName,
'netProfit' => 0,
'maxProfit' => 0,
];
}
$ddd[$monthName]['netProfit'] += $netProfit;
$ddd[$monthName]['maxProfit'] += $maxProfit;
Instead of
$dts['monthName'] = $dt['monthName'];
$dts['netProfit'] = $netProfit;
$dts['maxProfit'] = $maxProfit;
array_push($ddd, $dts);
To remove added keys, you could use :
$ddd = array_values($ddd);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments