doumu8911 2011-06-19 20:18
浏览 47
已采纳

两个数组,一个数组列出项目,第二个排序第一个,php代码如何?

So I got function that prints out array score which concert clinics:

array_id    clinic_id    score_amount
0             1              5
1             2              1
2             3              5
3             5              3
4             8              2

And i got another function that prints out array clinics id like this: This array is for listing all clinics on page,

array_id    clinic_id
0              1
1              2
2              3
3              4
4              5
5              6
6              7
7              8
8              9
9              10

Now my idea is to make array of clinic_id sorted accordinally to score_amount from first array. And theen if there is no score_amount for particular clinic_id, just go with order from second array normally.

So results needs to be sorted like:

array_id    clinic_id
0              1
1              3
2              5
3              8
4              2
5              4
6              6
7              7
8              9
9              10
  • 写回答

2条回答 默认 最新

  • dongyan9950 2011-06-19 20:31
    关注

    use a custom comparator,

    function cmp($a, $b)
    {
    if($a == $b) return 0;   
    \\get score returns score or -1 if no score exists
    if (getscore($a) == getscore($b) { 
    return (index($a) < index($b)) ? -1 : 1;  \\index is index of the number in second array     
    }
    return (getscore($a) < getscore($b)) ? -1 : 1;
    }
    
    usort($a, "cmp");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器