PHP 一个多维数组排序的问题

发布于 2022-09-04 20:47:45 字数 882 浏览 24 评论 0

这个问题有点类似 Mysql 中 order by ,需要的就是在数组中模拟对不同字段的排序。

假如有以下数组:

$beforeSort = [
    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];

现在需要在数组中按照 chinese 顺序,假如有相同,就按 math 顺序,最后得到的应该是如下的数组:

$afterSort = [
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
];

请问大家有什么不同的方法可以实现?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

也只是曾经 2022-09-11 20:47:45

这是我自己使用的版本,使用方法:

$afterSort = getArraySort($beforeSort, 'chinese', 'SORT_ASC', 'math', 'SORT_ASC');
/**
 * 二维数组排序(数字索引数组将重建索引)
 * @param array $arr 需要排序的数组 二维数组
 * @param string $arg1 排序的键名或字段名
 * @param string $arg2 排序的顺序 SORT_ASC或SORT_DESC
 * @param string $arg3 排序的方法 SORT_REGULAR
 * @return array
 */
function getArraySort($arr, $arg1, $arg2 = "SORT_ASC", $arg3 = "SORT_REGULAR")
{
    if (!is_array($arr) || !$arr)
    {
        return $arr;
    }
    $argcount = func_num_args();
    for ($i = 1; $i < $argcount; $i++)
    {
        $arg = func_get_arg($i);
        if (!preg_match("/SORT_(.*)/i", $arg))
        {
            $keynamelist[] = $arg;
            $sortrule[] = '
 . $arg;
        }
        else
        {
            $sortrule[] = $arg;
        }
    }
    foreach ($arr AS $key => $info)
    {
        foreach ($keynamelist AS $keyname)
        {
            ${$keyname}[$key] = $info[$keyname];
        }
    }
    $evalstring = 'array_multisort(' . join(",", $sortrule) . ',$arr);';
    eval($evalstring);
    return $arr;
}
梦罢 2022-09-11 20:47:45
    $beforeSort = [
        "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
        "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
        "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
    ];
    
    $arr = array();
    foreach($beforeSort as $value) {
        $arr[$value['chinese']][$value['math']] = $value;
    }
    sort($arr);
    
    $result = array();
    foreach($arr as $val) {
        sort($val);
        foreach($val as $vo) {
            $result[] = $vo;
        }
    }
    
    var_dump($result);die;
    

打印结果:

    array(3) {
      [0]=>
      array(4) {
        ["name"]=>
        string(6) "老王"
        ["english"]=>
        int(30)
        ["chinese"]=>
        int(50)
        ["math"]=>
        int(80)
      }
      [1]=>
      array(4) {
        ["name"]=>
        string(6) "张三"
        ["english"]=>
        int(80)
        ["chinese"]=>
        int(60)
        ["math"]=>
        int(50)
      }
      [2]=>
      array(4) {
        ["name"]=>
        string(6) "李四"
        ["english"]=>
        int(50)
        ["chinese"]=>
        int(60)
        ["math"]=>
        int(70)
      }
    }
    
    
莫言歌 2022-09-11 20:47:45

可以将数组转为集合,再处理。使用php的集合实现的sort方法,专治各种复杂排序

赤濁 2022-09-11 20:47:45

<?php
//现在需要在数组中按照 chinese 顺序,假如有相同,就按 math 顺序,最后得到的应该是如下的数组:
$beforeSort = [

"0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],

];

$data_math = array_column($beforeSort,'math');
$data_chinese = array_column($beforeSort,'chinese');
array_multisort($data_chinese,SORT_ASC,$data_math,SORT_ASC,$beforeSort);
print_r($beforeSort);

一梦等七年七年为一梦 2022-09-11 20:47:45
$beforeSort = [
    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 80 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];

usort($beforeSort, function($a, $b) {
    return [$a['chinese'], $a['math']] <=> [$b['chinese'], $b['math']];
});

var_dump($beforeSort);
我恋#小黄人 2022-09-11 20:47:45

///借用楼上哥们答案

usort($beforeSort, function ($a, $b) {
    return [$a['chinese'], $b['math']] <=> [$b['chinese'], $a['math']];
});
如梦 2022-09-11 20:47:45

对多维数组排序,官方有一个函数可以实现 array_multisort

无人问我粥可暖 2022-09-11 20:47:45
$beforeSort = [
    "0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 80 ],
    "1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
    "2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ]
];

foreach( $beforeSort as $key => $value )
{
    $chinese[$key] = $value['chinese'];
    $math[$key] = $value['math'];
}

array_multisort( $chinese, SORT_ASC, $math, SORT_DESC, $beforeSort );

echo '<pre>';
print_r($beforeSort);
三生一梦 2022-09-11 20:47:45

$beforeSort = [

"0"  => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1"  => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2"  => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],

];

foreach ($beforeSort as $key => $value) {

$chinese[$key] =  $value['chinese'];
$math[$key] =  $value['math'];

}
array_multisort($chinese, SORT_ASC, $math, SORT_ASC, $beforeSort);
print_r($beforeSort);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文