douyue9704 2012-11-16 11:34
浏览 9

Php sort 2数组键的方式相同

I'm struggling with a stupid issue i can't solve, probably because my head has just gone nuts. But really, i can't figure it out. It probably is simple than i think, but I wasn't able to find a solution on the web either. I have this array, sorted like i want:

array (size=3)
  9 => float 124.58194
  2 => float 52.8428
  8 => float 25.041806

Then, another array has the same keys, but different order

array (size=3)
  8 => string '3' (length=1)
  9 => string '3' (length=1)
  2 => string '2' (length=1)

Can i sort the second one by keys, using the order of the first one? If so, how?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongqie4402 2012-11-16 11:43
    关注

    Try this, I tested it, but it will kill any keys in disciple that aren't available in master.

    $master = array(
            9 => 999
        ,   2 => 222
        ,   8 => 888
    );
    
    $disciple = array(
            8 => 8989
        ,   9 => 9090
        ,   2 => 2020
    );
    
    $disciplined = discipline($master, $disciple);
    var_dump($disciplined);
    
    function discipline($master, $disciple) {
        $like_keys = array_intersect_key($master, $disciple);
        foreach($like_keys as $key => &$value) {
            $value = $disciple[$key];
        }
        return $like_keys;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。