dongqian3750 2014-05-22 20:42
浏览 48

在给定结构中的php中对多维数组进行排序

I have an array like this:

$myarray = array (
    array(
        array('Restaurant' =>
            array(
                'name' => 'Rodeo Ranch',
                'distance' => 1.2
            ),
        ),
        array('Menu' =>
            array(
                'title' => 'Steak with fries'
            )
        )
    ),
    array(
        array('Restaurant' =>
            array(
                'name' => 'City Bar',
                'distance' => 0.9
            ),
        ),
        array('Menu' =>
            array(
                'title' => 'Burger with Chicken'
            ),
            array(
                'title' => 'Hot-Dog'
            )
        )
    )
);

Now, I want to sort the array, that the nearest Restaurant is on top. I tried this:

$myarray = array_msort($myarray['Restaurant'], array('distance'=>SORT_ASC));

This does not work. Any help?

Unfortunately, I should not change the array because for further handling it needs to be in this structure.

Thank you for any hints and tips.

  • 写回答

2条回答 默认 最新

  • dty47696 2014-05-22 20:48
    关注

    You could use a closure:

    usort($myarray,function($a,$b) {
       if( $a['Restaraunt']['distance'] < $b['Restaraunt']['distance'] ) return -1;
       if( $a['Restaraunt']['distance'] > $b['Restaraunt']['distance'] ) return 1;
       return 0;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)