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 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义