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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?