开发者

Sort multi-dimensional array help

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
        开发者_Python百科    [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 );


Try this:

foreach ($array as $key => $val) {
    $newArr[$key] = $val['season_number'];
}
array_multisort($newArr, SORT_ASC, $array);

where $array is the array that you printed out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜