donvo24600 2014-09-08 05:36
浏览 22
已采纳

php按字母顺序排列多维数组

I have a multidimensional array that is created with this:

for($i=0; $i<count($sub_results);$i++)
{
    $presenters = array(
        'first_name' => $sub_results[$i]['data'][1]['user_value'],
        'last_name' => $sub_results[$i]['data'][2]['user_value'],
        'preferred_name' => $sub_results[$i]['data'][8]['user_value'],
        'organisation' => $sub_results[$i]['data'][11]['user_value'],
        'portraitFile' => $portrait_file_path.$portrait,
        'Abstract1title' => $sub_results[$i]['data'][19]['user_value'],
        'Abstract1accepted' => $sub_results[$i]['data'][18]['user_value'],
        'Abstract1synopsis' => $sub_results[$i]['data'][22]['user_value'],
        'Abstract1format' => $sub_results[$i]['data'][26]['user_value'],
        'Abstract1coauthors' => $sub_results[$i]['data'][20]['user_value'],
        'Abstract2title' => $sub_results[$i]['data'][28]['user_value'],
        'Abstract2accepted' => $sub_results[$i]['data'][27]['user_value'],
        'Abstract2synopsis' => $sub_results[$i]['data'][31]['user_value'],
        'Abstract2format' => $sub_results[$i]['data'][30]['user_value'],
        'Abstract2coauthors' => $sub_results[$i]['data'][29]['user_value']
    );
}

I want to sort the resulting array alphabetically by last_name. How is this achieved?

With thanks in advance, emrys

  • 写回答

1条回答 默认 最新

  • dongnius85154 2014-09-08 06:01
    关注

    This should do it for you. It's case-insensitive. If you have any questions about how it works let me know. Remember that usort accepts the array by reference, meaning that if you want to preserve the original array you will need to make a copy of it.

    <?php
        usort($array,function($a,$b) {
            $comparison = strcasecmp($a['last_name'],$b['last_name']);
            if ($comparison == 0) return 0;
            return ($comparison < 0) ? -1 : 1;
        });
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?