[英]Sort array rows by one column in tiers of 10, then by another column
How to do 2-tier sorting on an array using below criteria:如何使用以下标准对数组进行 2 层排序:
edd
value groups: edd
值组:
The array is数组是
$var_array = [
['name' => 'product1', 'edd'=>16, 'price' => 89],
['name' => 'product2', 'edd'=>21, 'price' => 99],
['name' => 'product3', 'edd'=>2, 'price' => 110],
['name' => 'product4', 'edd'=>14, 'price' => 102],
['name' => 'product5', 'edd'=>8, 'price' => 119],
['name' => 'product6', 'edd'=>6, 'price' => 123],
['name' => 'product7', 'edd'=>26, 'price' => 93],
['name' => 'product8', 'edd'=>27, 'price' => 105],
['name' => 'product9', 'edd'=>18, 'price' => 133],
];
First sort the edd
, and then sort the price
within each edd
group level.首先对
edd
进行排序,然后对每个edd
组级别内的price
进行排序。
Expected result预期结果
$var_array = [
['name' => 'product3', 'edd' => 2, 'price' => 110],
['name' => 'product5', 'edd' => 8, 'price' => 119],
['name' => 'product6', 'edd' => 6, 'price' => 123],
['name' => 'product1', 'edd' => 16, 'price' => 89],
['name' => 'product4', 'edd' => 14, 'price' => 102],
['name' => 'product9', 'edd' => 18, 'price' => 133],
['name' => 'product7', 'edd' => 26, 'price' => 93],
['name' => 'product2', 'edd' => 21, 'price' => 99],
['name' => 'product8', 'edd' => 27, 'price' => 105],
];
You can use array_reduce
and array_map
您可以使用
array_reduce
和array_map
$var_array = array(
array('name' => 'product1', 'edd'=>16, 'price' => 89),
array('name' => 'product2', 'edd'=>21, 'price' => 99),
array('name' => 'product3', 'edd'=>2, 'price' => 110),
array('name' => 'product4', 'edd'=>14, 'price' => 102),
array('name' => 'product5', 'edd'=>8, 'price' => 119),
array('name' => 'product6', 'edd'=>6, 'price' => 123),
array('name' => 'product7', 'edd'=>26, 'price' => 93),
array('name' => 'product8', 'edd'=>27, 'price' => 105),
array('name' => 'product9', 'edd'=>18, 'price' => 133),
);
//Group array and sort key
$temp = array_reduce($var_array, function($c, $v){
$c[ ceil($v["edd"] / 10) * 10 ][] = $v;
return $c;
}, array());
ksort($temp);
//Sort array
$temp = array_map(function ($n) {
usort($n, function($a, $b){
return $a["price"] - $b["price"];
});
return $n;
}, $temp );
//Make 2 dimentional array into 1
$result = array_reduce($temp, 'array_merge', array());
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to: 这将导致:
Array
(
[0] => Array
(
[name] => product3
[edd] => 2
[price] => 110
)
[1] => Array
(
[name] => product5
[edd] => 8
[price] => 119
)
[2] => Array
(
[name] => product6
[edd] => 6
[price] => 123
)
[3] => Array
(
[name] => product1
[edd] => 16
[price] => 89
)
[4] => Array
(
[name] => product4
[edd] => 14
[price] => 102
)
[5] => Array
(
[name] => product9
[edd] => 18
[price] => 133
)
[6] => Array
(
[name] => product7
[edd] => 26
[price] => 93
)
[7] => Array
(
[name] => product2
[edd] => 21
[price] => 99
)
[8] => Array
(
[name] => product8
[edd] => 27
[price] => 105
)
)
This 2-rule sort can be simply done with usort()
.这种 2 规则排序可以简单地使用
usort()
完成。 Use ceil($number / 10)
to make groups of 1 - 10, 11 - 20, etc. I want 0 - 9, 10 - 19, 20 - 21, use intdiv($number, 10)
.使用
ceil($number / 10)
组成 1 - 10、11 - 20 等组。我想要 0 - 9、10 - 19、20 - 21,使用intdiv($number, 10)
。
edd
values ascending, thenedd
值的下限的十分之一排序,然后price
value ascending.price
值升序排序。usort(
$var_array,
fn($a, $b) => [ceil($a['edd'] / 10), $a['price']]
<=>
[ceil($b['edd'] / 10), $b['price']]
);
However, it will be more efficient to use array_multsort()
because fewer function calls will be needed.但是,使用
array_multsort()
会更有效,因为需要更少的 function 调用。
$groups = [];
$prices = [];
foreach ($var_array as $row) {
$groups[] = ceil($row['edd'] / 10);
$prices[] = $row['price'];
}
array_multisort($groups, $prices, $var_array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.