簡體   English   中英

按 10 層的一列對數組行進行排序,然后再按另一列

[英]Sort array rows by one column in tiers of 10, then by another column

如何使用以下標准對數組進行 2 層排序:

edd值組:

  1. 1-10
  2. 11-20
  3. 21-30

數組是

$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_reducearray_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)

  1. 按上升的edd值的下限的十分之一排序,然后
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM