我得到了以下数组:
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
)
)
)
正如您所看到的,数组是按产品线分组的,我想要实现的是按DESC顺序对每个产品线的quantiysold对数组进行排序。这意味着结果应该是。
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
)
)
)
请给我提供一些解决方案:)
查看sort
函数的PHP文档,您正在寻找的算法是由用户在贡献中提供的:
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;
}
使用它,只需调用:array_sort($yourArray, 'QuantitySold', SORT_DESC)