[英]Sort array rows by one column in tiers of 10, then by another column
如何使用以下标准对数组进行 2 层排序:
edd
值组:
数组是
$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],
];
首先对edd
进行排序,然后对每个edd
组级别内的price
进行排序。
预期结果
$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],
];
您可以使用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>";
这将导致:
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
)
)
这种 2 规则排序可以简单地使用usort()
完成。 使用ceil($number / 10)
组成 1 - 10、11 - 20 等组。我想要 0 - 9、10 - 19、20 - 21,使用intdiv($number, 10)
。
edd
值的下限的十分之一排序,然后price
值升序排序。代码:(演示)
usort(
$var_array,
fn($a, $b) => [ceil($a['edd'] / 10), $a['price']]
<=>
[ceil($b['edd'] / 10), $b['price']]
);
但是,使用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.