PHP数组多列数据排序[replicate]

9gm1akwq  于 2023-02-03  发布在  PHP
关注(0)|答案(3)|浏览(118)
    • 此问题在此处已有答案**:

How do I sort a multidimensional array by one of the fields of the inner array in PHP? [duplicate](8个答案)
十小时前关门了。
我想做一个数组排序方法,这样你就可以排序一些信息通过价格/身份证号码等。
我的数组是这样的:

[1] 1002234985, $123.00, ITEM DESCRIPTION #1

[2] 1034234985, $143.70, ITEM DESCRIPTION #2

[3] 1002467455, $133.06, ITEM DESCRIPTION #3

[4] 1564334985, $883.11, ITEM DESCRIPTION #4

我想按价格对数组进行排序,但是要使描述和ID号匹配。我该怎么做?
谢谢!

jjjwad0x

jjjwad0x1#

编写compare函数(接收两个元素并返回较大的元素),然后使用usort来使用它
举个例子:

$arr = array(
              array(1002234985, '125.00', 'ITEM DESCRIPTION'), 
              array(1002234986, '124.00', 'ITEM DESCRIPTION'), 
              array(1002234987, '123.00', 'ITEM DESCRIPTION')
);
function mycomp($itm1, $itm2){
    if($itm1[1] > $itm2[1]){
        return 1;
    }
    else if($itm1[1] < $itm2[1]){
        return -1;
    }
    else{
        return 0;
    }
}
usort($arr, 'mycomp');
print_r($arr);
u3r8eeie

u3r8eeie2#

下面是一个使用PHP的usort对多维数组进行排序的示例。

// Change this to the index of the key you wish to sort by
$sortBy = 0;

$myArray = array(
  array(1002234985, '$123.00', 'ITEM DESCRIPTION #1'),
  array(1034234985, '$143.70', 'ITEM DESCRIPTION #2'),
  array(1002467455, '$133.06', 'ITEM DESCRIPTION #3'),
  array(1564334985, '$883.11', 'ITEM DESCRIPTION #4')
];

usort($myArray, 'cmp');

function cmp($a, $b){
  if ($a[$sortBy] == $b[$sortBy]) {
    return 0;
  }
  return ($a[$sortBy] < $b[$sortBy]) ? -1 : 1;
}
bd1hkmkf

bd1hkmkf3#

请使用以下功能:

function sortArrayByTwoKeys($data, $k1, $k2, $order='ASC'){
    if($order == 'DESC'){
        usort($data, function (array $a, array $b) {
            return [$b['score'], $b['name']] <=> [$a['score'], $a['name']];
        });
    } elseif($order == 'ASC_K2_DESC_K1'){
        usort($data, function (array $a, array $b) {
            return [$b['score'], $a['name']] <=> [$a['score'], $b['name']];
        });
    } elseif($order == 'ASC_K1_DESC_K2'){
        usort($data, function (array $a, array $b) {
            return [$a['score'], $b['name']] <=> [$b['score'], $a['name']];
        });
    } else {//ASC
        usort($data, function (array $a, array $b) {
            return [$a['score'], $a['name']] <=> [$b['score'], $b['name']];
        });
    }
    return $data;
}
//Example 1
$data = [
['id' => 111, 'score' => 5, 'name' => 'xyz'],
['id' => 222, 'score' => 3, 'name' => 'ddd'],
['id' => 333, 'score' => 2, 'name' => 'xyz'],
['id' => 444, 'score' => 3, 'name' => 'fff'],
['id' => 555, 'score' => 5, 'name' => 'acb'],
['id' => 666, 'score' => 6, 'name' => 'ttt'],
['id' => 777, 'score' => 6, 'name' => 'sss'],
['id' => 888, 'score' => 6, 'name' => 'aaa']
];
$data = sortArrayByTwoKeys($data, 'score', 'name', 'ASC_K2_DESC_K1');
echo '<pre>';print_r($data);echo '</pre>';

输出将为:数组([0] =〉数组([id] =〉888 [分数] =〉6 [名称] =〉aaa)

[1] => Array
    (
        [id] => 777
        [score] => 6
        [name] => sss
    )

[2] => Array
    (
        [id] => 666
        [score] => 6
        [name] => ttt
    )

[3] => Array
    (
        [id] => 555
        [score] => 5
        [name] => acb
    )

[4] => Array
    (
        [id] => 111
        [score] => 5
        [name] => xyz
    )

[5] => Array
    (
        [id] => 222
        [score] => 3
        [name] => ddd
    )

[6] => Array
    (
        [id] => 444
        [score] => 3
        [name] => fff
    )

[7] => Array
    (
        [id] => 333
        [score] => 2
        [name] => xyz
    )

相关问题