-1

I have got the following array:

Array
(

[Exo Wetline FOR April:2014] => Array
    (
        [0] => Array
            (
                [ProductRowId] => 2000453
                [SKU] => P2000453
                [ProductLineName] => Exo Wetline
                [ProductName] => High Degree 16 oz.
                [ProductSize] => 16
                [LastSoldDate] => 04/16/14
                [AvgUnitPrice] => 17.99
                [AvgSellingPrice] => 17.99
                [AvgUnitCost] => 17.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 2
                [PreDiscountTotal] => 35.98
                [TotalDiscount] => 6.31
                [TotalSales] => 29.67
                [SoldDate] => 04/01/14
            )

        [1] => Array
            (
                [ProductRowId] => 2000451
                [SKU] => P2000451
                [ProductLineName] => Exo Wetline
                [ProductName] => Top Notch 32 oz.
                [ProductSize] => 32
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 28.99
                [AvgSellingPrice] => 28.99
                [AvgUnitCost] => 28.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 1
                [PreDiscountTotal] => 28.99
                [TotalDiscount] => 8.8
                [TotalSales] => 20.19
                [SoldDate] => 04/01/14
            )

        [2] => Array
            (
                [ProductRowId] => 2000466
                [SKU] => P2000466
                [ProductLineName] => Exo Wetline
                [ProductName] => Lasting Impressions 8oz
                [ProductSize] => 8
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 14.99
                [AvgSellingPrice] => 14.99
                [AvgUnitCost] => 14.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 6
                [PreDiscountTotal] => 89.94
                [TotalDiscount] => 16.57
                [TotalSales] => 73.37
                [SoldDate] => 04/01/14
            )

        [3] => Array
            (
                [ProductRowId] => 2000442
                [SKU] => P2000442
                [ProductLineName] => Exo Wetline
                [ProductName] => Transform 32 oz.
                [ProductSize] => 32
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 28.99
                [AvgSellingPrice] => 28.99
                [AvgUnitCost] => 28.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 1
                [PreDiscountTotal] => 28.99
                [TotalDiscount] => 8.8
                [TotalSales] => 20.19
                [SoldDate] => 04/01/14
            )

    )

 [Exo Wetline FOR May:2014] => Array
    (
        [0] => Array
            (
                [ProductRowId] => 2000453
                [SKU] => P2000453
                [ProductLineName] => Exo Wetline
                [ProductName] => High Degree 16 oz.
                [ProductSize] => 16
                [LastSoldDate] => 04/16/14
                [AvgUnitPrice] => 17.99
                [AvgSellingPrice] => 17.99
                [AvgUnitCost] => 17.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 1
                [PreDiscountTotal] => 35.98
                [TotalDiscount] => 6.31
                [TotalSales] => 29.67
                [SoldDate] => 04/01/14
            )

[1] => Array
            (
                [ProductRowId] => 2000442
                [SKU] => P2000442
                [ProductLineName] => Exo Wetline
                [ProductName] => Transform 32 oz.
                [ProductSize] => 32
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 28.99
                [AvgSellingPrice] => 28.99
                [AvgUnitCost] => 28.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 3
                [PreDiscountTotal] => 28.99
                [TotalDiscount] => 8.8
                [TotalSales] => 20.19
                [SoldDate] => 04/01/14
            )
   )

)

as you can see the array is grouped by the product line wise , what i want to achieve is to sort the array by QuantitySold for every individual product line in DESC Order.that means the result should be.

Array
(

[Exo Wetline FOR April:2014] => Array
    (

[0] => Array
            (
                [ProductRowId] => 2000466
                [SKU] => P2000466
                [ProductLineName] => Exo Wetline
                [ProductName] => Lasting Impressions 8oz
                [ProductSize] => 8
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 14.99
                [AvgSellingPrice] => 14.99
                [AvgUnitCost] => 14.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 6
                [PreDiscountTotal] => 89.94
                [TotalDiscount] => 16.57
                [TotalSales] => 73.37
                [SoldDate] => 04/01/14
            )


        [1] => Array
            (
                [ProductRowId] => 2000453
                [SKU] => P2000453
                [ProductLineName] => Exo Wetline
                [ProductName] => High Degree 16 oz.
                [ProductSize] => 16
                [LastSoldDate] => 04/16/14
                [AvgUnitPrice] => 17.99
                [AvgSellingPrice] => 17.99
                [AvgUnitCost] => 17.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 2
                [PreDiscountTotal] => 35.98
                [TotalDiscount] => 6.31
                [TotalSales] => 29.67
                [SoldDate] => 04/01/14
            )

        [2] => Array
            (
                [ProductRowId] => 2000451
                [SKU] => P2000451
                [ProductLineName] => Exo Wetline
                [ProductName] => Top Notch 32 oz.
                [ProductSize] => 32
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 28.99
                [AvgSellingPrice] => 28.99
                [AvgUnitCost] => 28.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 1
                [PreDiscountTotal] => 28.99
                [TotalDiscount] => 8.8
                [TotalSales] => 20.19
                [SoldDate] => 04/01/14
            )



        [3] => Array
            (
                [ProductRowId] => 2000442
                [SKU] => P2000442
                [ProductLineName] => Exo Wetline
                [ProductName] => Transform 32 oz.
                [ProductSize] => 32
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 28.99
                [AvgSellingPrice] => 28.99
                [AvgUnitCost] => 28.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 1
                [PreDiscountTotal] => 28.99
                [TotalDiscount] => 8.8
                [TotalSales] => 20.19
                [SoldDate] => 04/01/14
            )

    )

 [Exo Wetline FOR May:2014] => Array
    (

 [0] => Array
            (
                [ProductRowId] => 2000442
                [SKU] => P2000442
                [ProductLineName] => Exo Wetline
                [ProductName] => Transform 32 oz.
                [ProductSize] => 32
                [LastSoldDate] => 04/30/14
                [AvgUnitPrice] => 28.99
                [AvgSellingPrice] => 28.99
                [AvgUnitCost] => 28.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 3
                [PreDiscountTotal] => 28.99
                [TotalDiscount] => 8.8
                [TotalSales] => 20.19
                [SoldDate] => 04/01/14
            )

        [1] => Array
            (
                [ProductRowId] => 2000453
                [SKU] => P2000453
                [ProductLineName] => Exo Wetline
                [ProductName] => High Degree 16 oz.
                [ProductSize] => 16
                [LastSoldDate] => 04/16/14
                [AvgUnitPrice] => 17.99
                [AvgSellingPrice] => 17.99
                [AvgUnitCost] => 17.99
                [ExpectedMargin] => 0
                [ActualMargin] => 0
                [QuantitySold] => 1
                [PreDiscountTotal] => 35.98
                [TotalDiscount] => 6.31
                [TotalSales] => 29.67
                [SoldDate] => 04/01/14
            )


   )

)  

Please provide me with some solution :)

superphonic
  • 7,954
  • 6
  • 30
  • 63
laracaster
  • 31
  • 3
  • 11

1 Answers1

0

Have a look at the PHP documentation of sort function, the algorithm you're looking for is provided by a user in the contributions:

function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

To use it, just call: array_sort($yourArray, 'QuantitySold', SORT_DESC)

Tip-Sy
  • 810
  • 10
  • 18