php将两个二维数组合并并根据某个字段排序

1.需要用到的几个php函数

1.1 array_merge() 函数用于把一个或多个数组合并为一个数组。

        PHP array_merge() 函数 | 菜鸟教程PHP array_merge() 函数 完整的 PHP Array 参考手册 实例 把两个数组合并为一个数组: [mycode3 type='php'] [/mycode3] 运行实例 » 定义和用法 array_merge() 函数用于把一个或多个数组合并为一个数组。 提示:您可以向函数输入一个或者多个数组。 注释:如果两个或更多个数组元素有相同的键名,则最后的元素会覆盖其他元素。 注释:如果您仅..https://www.runoob.com/php/func-array-merge.html

1.2 array_column() 返回输入数组中某个单一列的值。

PHP array_column() 函数 | 菜鸟教程PHP array_column() 函数 完整的 PHP Array 参考手册 实例 从记录集中取出 last_name 列: <?php// 可能从数据库中返回数组$a = array(  array(    'id' => 5698,    https://www.runoob.com/php/func-array-column.html

1.3 array_multisort函数对多个数组或多维数组进行排序(这里我们只需要用到对多维数组进行排序的功能)

PHP数组之array_multisort函数详解-php教程-PHP中文网PHP中array_multisort函数对多个数组或多维数组进行排序,参数数组被当成一个表的列并以行来排序——这类似于 SQL 的 ORDER BY 子句的功能。第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的大小来排序,依此类推。注意:作为参数的数组元素个数应保持一致,否则会报错。https://www.php.cn/php-weizijiaocheng-390254.html

2.实际操作。

2.1 我这里是thinkphp框架的查询语法,只需看1和2的业务操作即可

        //作业管理
        $data1 = Db::name('operating_manage')->where(['operating_status'=>1])->whereTime('create_time','today')->field('car_no,wh_name as storage_location,create_time')->order('id desc')->select();
        //吊车作业
        $data2 = Db::name('operating_crane')->alias('a')
            ->join('sys_dict b','a.goods_name = b.k')
            ->where(['a.operating_status'=>1])
            ->whereTime('a.create_time','today')
            ->field('a.media_information as car_no,b.name as storage_location,a.create_time')
            ->order('a.id desc')
            ->select();

        //1.将两个二维数组合并
        $data = array_merge($data1, $data2); //两个二维数组合并
        //2.根据字段last_name对数组$data进行降序排列
        $sort_arr = array_column($data,'create_time'); //获取出来的是一维数组
        array_multisort($sort_arr,SORT_ASC,$data);  //升序排序
        
        //输出打印
        dump($data1);
        dump($data2);
        dump($data);

2.2 打印结果

array(2) {
  [0] => array(3) {
    ["car_no"] => string(9) "蒙L19758"
    ["storage_location"] => string(16) "1号煤棚诺恩"
    ["create_time"] => string(19) "2022-08-03 18:18:04"
  }
  [1] => array(3) {
    ["car_no"] => string(9) "蒙L17135"
    ["storage_location"] => string(16) "1号煤棚诺恩"
    ["create_time"] => string(19) "2022-08-03 10:37:58"
  }
}
array(2) {
  [0] => array(3) {
    ["car_no"] => string(9) "蒙B90093"
    ["storage_location"] => string(10) "测试1号"
    ["create_time"] => string(19) "2022-08-03 16:24:54"
  }
  [1] => array(3) {
    ["car_no"] => string(9) "蒙B90093"
    ["storage_location"] => string(10) "测试1号"
    ["create_time"] => string(19) "2022-08-03 16:24:54"
  }
}
array(4) {
  [0] => array(3) {
    ["car_no"] => string(9) "蒙L17135"
    ["storage_location"] => string(16) "1号煤棚诺恩"
    ["create_time"] => string(19) "2022-08-03 10:37:58"
  }
  [1] => array(3) {
    ["car_no"] => string(9) "蒙B90093"
    ["storage_location"] => string(10) "测试1号"
    ["create_time"] => string(19) "2022-08-03 16:24:54"
  }
  [2] => array(3) {
    ["car_no"] => string(9) "蒙B90093"
    ["storage_location"] => string(10) "测试1号"
    ["create_time"] => string(19) "2022-08-03 16:24:54"
  }
  [3] => array(3) {
    ["car_no"] => string(9) "蒙L19758"
    ["storage_location"] => string(16) "1号煤棚诺恩"
    ["create_time"] => string(19) "2022-08-03 18:18:04"
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值