基于两个键

时间:2017-03-23 10:41:43

标签: php arrays sorting

我想通过将带有空值的数组记录推送到底部来对id字段进行排序。如果存在id值,则按asc。

对col_1进行排序

我有一个像这样的多维数组。

Array
(
  [0]=> Array
        (
          [id]=>1166
          [col_1]=>9.4
        )
  [1]=> Array
        (
          [id]=>
          [col_1]=>2.4
        )
  [2]=> Array
        (
          [id]=>1012
          [col_1]=>0.96
        )
  [3]=> Array
        (
          [id]=>1856
          [col_1]=>7.47
        )
)

我想按id和col_1排序。 我试过了

foreach ($arr as $key => $row) {
  $x[$key]  = $row['id'];
  $y[$key]  = $row['col_1'];
}
array_multisort($x, SORT_DESC, $y, SORT_ASC, $arr);

我得到以下结果

Array
(
  [0]=> Array
        (
          [id]=>1856
          [col_1]=>7.47
        )
  [1]=> Array
        (
          [id]=>1166
          [col_1]=>9.4
        )
  [2]=> Array
        (
          [id]=>1012,
          [col_1]=>0.96
        )
  [3]=> Array
        (
          [id]=>
          [col_1]=>2.47
        )
)

但我希望结果为,

Array
(
  [0]=> Array
        (
          [id]=>1012
          [col_1]=>0.96
        )
  [1]=> Array
        (
          [id]=>1856
          [col_1]=>7.47
        )
  [2]=> Array
        (
          [id]=>1166
          [col_1]=>9.4
        )
  [3]=> Array
        (
          [id]=>
          [col_1]=>2.4
        )
)

' ID'对于某些记录,字段也可以为null。但col_1将用于所有记录。

2 个答案:

答案 0 :(得分:2)

不完全复杂,但可读且直截了当:

<?php
$data = [
    [
        'id' => 1166,
        'col_1' => 9.4
    ],
    [
        'id' => null,
        'col_1' => 2.4
    ],
    [
        'id' => 1012,
        'col_1' => 0.96
    ],
    [
        'id' => 1856,
        'col_1' => 7.47
    ]
];
usort($data, function($a, $b) {
    return ($a['col_1']>$b['col_1']);
});
usort($data, function($a) {
    return !isset($a['id']);
});
print_r($data);

输出显然是:

Array
(
    [0] => Array
        (
            [id] => 1012
            [col_1] => 0.96
        )

    [1] => Array
        (
            [id] => 1856
            [col_1] => 7.47
        )

    [2] => Array
        (
            [id] => 1166
            [col_1] => 9.4
        )

    [3] => Array
        (
            [id] =>
            [col_1] => 2.4
        )

)

在下面的评论中,您现在需要额外的要求。我怀疑在直接排序方法中有直接的方法来评估两个要求。那么为什么不尝试一个可读且明显的一个:

<?php
$data = [
    [
        'id' => 1166,
        'col_1' => 9.4
    ],
    [
        'id' => null,
        'col_1' => 2.4
    ],
    [
        'id' => null,
        'col_1' => 0.2
    ],
    [
        'id' => 1012,
        'col_1' => 0.96
    ],
    [
        'id' => null,
        'col_1' => 12
    ],
    [
        'id' => 1856,
        'col_1' => 7.47
    ]
];
usort($data, function($a, $b) {
    return ($a['col_1']>$b['col_1']);
});
$withId = array_filter($data, function($entry) {
    return isset($entry ['id']);
});
$withoutId = array_filter($data, function($entry) {
    return !isset($entry ['id']);
});
$data = array_merge($withId, $withoutId);
print_r($data);

输出显然是:

Array
(
    [0] => Array
        (
            [id] => 1012
            [col_1] => 0.96
        )

    [1] => Array
        (
            [id] => 1856
            [col_1] => 7.47
        )

    [2] => Array
        (
            [id] => 1166
            [col_1] => 9.4
        )

    [3] => Array
        (
            [id] =>
            [col_1] => 0.2
        )

    [4] => Array
        (
            [id] =>
            [col_1] => 2.4
        )

    [5] => Array
        (
            [id] =>
            [col_1] => 12
        )

)

答案 1 :(得分:1)

您可以使用usort函数进行自定义排序。

 function customSort($a, $b) {
    if (null === $a['id'] && null === $b['id']) {
        return 0;
    }
    if (null === $a['id']) {
        return 1;
    } 
    if (null === $b['id']) {
        return -1;
    }
    return ($a['col_1'] < $b['col_1']) ? -1 : 1;
}

$array = [
    [
        'id' => 1012,
        'col_1' => 0.96
        ],
    [
        'id' => 1856,
        'col_1' => 7.47
        ],
            [
        'id' => null,
        'col_1' => 2.4
        ],
    [
        'id' => 1166,
        'col_1' => 9.4
        ],

    ];



    usort($array, 'customSort');
    print_r($array);

返回

Array
(
    [0] => Array
        (
            [id] => 1012
            [col_1] => 0.96
        )

    [1] => Array
        (
            [id] => 1856
            [col_1] => 7.47
        )

    [2] => Array
        (
            [id] => 1166
            [col_1] => 9.4
        )

    [3] => Array
        (
            [id] => 
            [col_1] => 2.4
        )

)

这是你的期望。希望这有帮助。