排序多维数组的帮助 [英] Sort multi-dimensional array help

查看:113
本文介绍了排序多维数组的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决这阵由 season_number ,但是我真的不知道该充当我想我需要自定义排序使用?任何想法?

 阵列

    [0] =>排列
        (
            [season_number] => 7
            [show_id] => 21
            [show_seasons_id] => 14
        )    [1] =>排列
        (
            [season_number] => 6
            [show_id] => 21
            [show_seasons_id] => 31
        )    [2] =>排列
        (
            [season_number] => 1
            [show_id] => 21
            [show_seasons_id] => 40
        )    [3] =>排列
        (
            [season_number] => 2
            [show_id] => 21
            [show_seasons_id] => 41
        )


解决方案

您可以使用的 usort 用比较函数功能:

 函数compare_my_elements($ ARR1,$ ARR2){
   $ S1 = $ ARR1 [season_number];
   $ S2 = $ ARR2 [season_number];
   如果($ S1 == $ S2)返回0;
   返回($ S1> $ S2?1:-1);
}usort($ my_md_array,compare_my_elements);

I am trying to sort this array by season_number however I am not really sure which function to use as I assume I need a custom sort? Any ideas?

Array
(
    [0] => Array
        (
            [season_number] => 7
            [show_id] => 21
            [show_seasons_id] => 14
        )

    [1] => Array
        (
            [season_number] => 6
            [show_id] => 21
            [show_seasons_id] => 31
        )

    [2] => Array
        (
            [season_number] => 1
            [show_id] => 21
            [show_seasons_id] => 40
        )

    [3] => Array
        (
            [season_number] => 2
            [show_id] => 21
            [show_seasons_id] => 41
        )
)

解决方案

You can use the usort function with the 'compare' function:

function compare_my_elements( $arr1, $arr2 ) {
   $s1=$arr1["season_number"];
   $s2=$arr2["season_number"];
   if( $s1 == $s2 ) return 0;
   return ( $s1 > $s2 ? 1 : -1 );
}

usort( $my_md_array, compare_my_elements );

这篇关于排序多维数组的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆