duandu1915 2011-11-25 03:39 采纳率: 100%
浏览 40
已采纳

如何使用自定义或用户定义的标准在php中合并和排序数组?

I have two array :

$data1 = array( 
        (0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
        (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
        (2) => array("level" => 1, "id" => 3, "index" => 1, "amount" => 0));  

$data2 = array( 
        (0) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
        (1) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
        (2) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
        (3) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000),
        (4) => array("level" => 2, "id" => 3, "index" => 1, "amount" => 0));

I want to merge those array into one array and the result is like this :

$expected = array(
         (0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
         (1) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
         (2) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
         (3) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
         (4) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
         (5) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000));

I've tried to use array_merge_recursive :

$try = array_merge_recursive($data1, $data2);  

But the result is like this :

$try = array(
         (0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
         (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
         (3) => array("level" => 1, "id" => 3, "index" => 1, "amount" => 0),
         (4) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
         (5) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),             
         (6) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
         (7) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000),
         (8) => array("level" => 2, "id" => 3, "index" => 1, "amount" => 0));

I have criteria for my result :

  1. Remove array where amount=0
  2. Sort and order by id,level,index

I've read about uasort and usort but i dont have any idea how to implement that function to match what i need. Is that function (uasort/usort) can solve it or any other idea? Please help me, thanks!

  • 写回答

2条回答 默认 最新

  • dongzhunqiu4841 2011-11-25 03:50
    关注
    $data = array_merge($data1, $data2);
    $data = array_filter($data, function ($d) {
        return $d['amount'] != 0;
    });
    usort($data, function ($a, $b) {
        if ($a['id'] != $b['id'])       return $a['id'] - $b['id'];
        if ($a['level'] != $b['level']) return $a['level'] - $b['level'];
        else                            return $a['index'] - $b['index'];
    });
    

    Note: Uses PHP 5.3+ anonymous function syntax.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要