How to group the array by using array value, PHP

Sajid Farooqui

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

Syscall

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to group the array by using array value, PHP

From Dev

How to print array value into non array value using php

From Dev

How to print array value into non array value using php

From Dev

How to search string for value using array in PHP?

From Dev

How to summary same value in array using PHP?

From Dev

How to search string for value using array in PHP?

From Dev

Group and Limit Array Using PHP

From Dev

How to split given array and get value from array using php

From Dev

php array how get in array value for using foreach

From Dev

PHP how to group a multidimensional array which value is same

From Dev

How to group PHP array result

From Dev

PHP JSON Array - Group by the same value

From Dev

PHP JSON Array - Group by the same value

From Dev

PHP Multidimensional Array loop and group by value

From Dev

PHP Convert string to array and group by value

From Dev

PHP MySQL Group By Using given array

From Dev

PHP how to populate array from array by value

From Dev

How to combine and remove duplicate array value using PHP?

From Dev

How to insert mulitple POST value from array using PHP

From Dev

How can get array key value from object using php

From Dev

How to find out if the key value is existed in an array using PHP?

From Dev

How to multiply and round only one value of an array with subarrays using php?

From Dev

how to get exact key's value in array using PHP?

From Dev

How to sort array data as per key value using PHP?

From Dev

How to change query response array value using MongoDB and PHP?

From Dev

how to sort array based on value using php codeigniter in the fastest way?

From Dev

How to assign array variable using explode value in PHP?

From Dev

How to pass select box value with array and store in Database using php

From Dev

How to find key where value is array with certain key using php?

Related Related

  1. 1

    How to group the array by using array value, PHP

  2. 2

    How to print array value into non array value using php

  3. 3

    How to print array value into non array value using php

  4. 4

    How to search string for value using array in PHP?

  5. 5

    How to summary same value in array using PHP?

  6. 6

    How to search string for value using array in PHP?

  7. 7

    Group and Limit Array Using PHP

  8. 8

    How to split given array and get value from array using php

  9. 9

    php array how get in array value for using foreach

  10. 10

    PHP how to group a multidimensional array which value is same

  11. 11

    How to group PHP array result

  12. 12

    PHP JSON Array - Group by the same value

  13. 13

    PHP JSON Array - Group by the same value

  14. 14

    PHP Multidimensional Array loop and group by value

  15. 15

    PHP Convert string to array and group by value

  16. 16

    PHP MySQL Group By Using given array

  17. 17

    PHP how to populate array from array by value

  18. 18

    How to combine and remove duplicate array value using PHP?

  19. 19

    How to insert mulitple POST value from array using PHP

  20. 20

    How can get array key value from object using php

  21. 21

    How to find out if the key value is existed in an array using PHP?

  22. 22

    How to multiply and round only one value of an array with subarrays using php?

  23. 23

    how to get exact key's value in array using PHP?

  24. 24

    How to sort array data as per key value using PHP?

  25. 25

    How to change query response array value using MongoDB and PHP?

  26. 26

    how to sort array based on value using php codeigniter in the fastest way?

  27. 27

    How to assign array variable using explode value in PHP?

  28. 28

    How to pass select box value with array and store in Database using php

  29. 29

    How to find key where value is array with certain key using php?

HotTag

Archive