dqhgjay5753 2015-05-14 19:37
浏览 49
已采纳

合并和排序关联数组

First array:

Array ( 
    [0] => Array ( [id] => 1 [occ] => 14 ) 
    [1] => Array ( [id] => 2 [occ] => 12 ) 
    [2] => Array ( [id] => 4 [occ] => 2 ) 
)

Second array:

Array ( 
    [0] => Array ( [id] => 1 [company_name] => Google   [CEO] => Mike ) 
    [1] => Array ( [id] => 2 [company_name] => Apple    [CEO] => Jones) 
    [2] => Array ( [id] => 2 [company_name] => Bmw      [CEO] => Steve) 
    [3] => Array ( [id] => 3 [company_name] => Hardsoft [CEO] => Lucy ) 
    [4] => Array ( [id] => 4 [company_name] => Lays     [CEO] => Morty) 
)

I Would like to merge these arrays to something like that:

Array ( 
    [0] => Array ( [id] => 1 [company_name] => Google   [CEO] => Mike   [occ] => 14) 
    [1] => Array ( [id] => 2 [company_name] => Apple    [CEO] => Jones  [occ] => 12) 
    [2] => Array ( [id] => 3 [company_name] => Bmw      [CEO] => Steve  [occ] => 0) 
    [3] => Array ( [id] => 4 [company_name] => Hardsoft [CEO] => Lucy   [occ] => 2) 
    [4] => Array ( [id] => 5 [company_name] => Lays     [CEO] => Morty  [occ] => 0) 
)

Then sort them by occ so Google will be first, Apple second, Hardsoft third and so on.

How can I archive it?

  • 写回答

4条回答 默认 最新

  • 普通网友 2015-05-14 20:16
    关注

    This should work for you:

    First of I use array_column() to get an array with the id's from the first array as "lookup table". Then I use array_values() to reindex the second array.

    After this I start with looping through the second array and reassigning the id, by simply using the key from the innerArray + 1. So that you don't have duplicate id's.

    Then I look with array_search() if the new id is in the lookup table and if yes I assign the value occ from the first array to the second array. If not I simply assign 0 as value of the key occ in the second array.

    After that is all done, it is a simple sort with usort() to compare the values of occ and sort it by it's value (Note: If you want to change the sorting from DESC to ASC just simply change < to > in the usort() call).

    <?php
    
        $ids = array_column($arr1, "id");
        $arr2 = array_values($arr2);
    
        foreach($arr2 as $k => &$v) {
            $v["id"] = ($k+1);
            if(in_array($v["id"], $ids))
                $arr2[$k]["occ"] = $arr1[array_search($v["id"], $ids)]["occ"];
            else
                $arr2[$k]["occ"] = 0;
        }           
    
        usort($arr2, function($a, $b){
            if($a["occ"] == $b["occ"])
                return 0;
            return $a["occ"] < $b["occ"] ? 1 : -1;
        });
    
        print_r($arr2);
    
    ?>
    

    output:

    Array
    (
        [0] => Array
            (
                [id] => 1
                [company_name] => Google
                [CEO] => Mike
                [occ] => 14
            )
    
        //...
    
        [4] => Array
            (
                [id] => 3
                [company_name] => Bmw
                [CEO] => Steve
                [occ] => 0
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,