首頁 > 後端開發 > php教程 > php二维数组中按某个元素的值排序(升序) 求大神支招

php二维数组中按某个元素的值排序(升序) 求大神支招

WBOY
發布: 2016-06-06 20:37:30
原創
1117 人瀏覽過

按照birthday_interval升序排序

<code>Array
(
    [0] => Array
        (
            [birthday_range] => 三月外
            [id] => 8
            [age] => 24
            [birthday] => 12月04日
            [username] => haha
            [birthday_interval] => 273
        )

    [1] => Array
        (
            [birthday_range] => 三月外
            [id] => 9
            [age] => 24
            [birthday] => 12月04日
            [username] => haha
            [birthday_interval] => 283
        )

    [2] => Array
        (
            [birthday_range] => 三月内
            [id] => 10
            [age] => 16
            [birthday] => 04月12日
            [username] => 123
            [birthday_interval] => 37
        )

    [3] => Array
        (
            [birthday_range] => 三月内
            [id] => 11
            [age] => 16
            [birthday] => 04月12日
            [username] => xcvxcvxcv
            [birthday_interval] => 37
        )

    [4] => Array
        (
            [birthday_range] => 三月内
            [id] => 12
            [age] => 16
            [birthday] => 04月12日
            [username] => asasdfgh
            [birthday_interval] => 37
        )

    [5] => Array
        (
            [birthday_range] => 三月内
            [id] => 13
            [age] => 16
            [birthday] => 04月12日
            [username] => 1123
            [birthday_interval] => 32
        )

    [6] => Array
        (
            [birthday_range] => 三月内
            [id] => 14
            [age] => 16
            [birthday] => 04月12日
            [username] => 123
            [birthday_interval] => 31
        )

    [7] => Array
        (
            [birthday_range] => 三月内
            [id] => 15
            [age] => 16
            [birthday] => 04月12日
            [username] => 123
            [birthday_interval] => 37
        )

    [8] => Array
        (
            [birthday_range] => 三月内
            [id] => 16
            [age] => 21
            [birthday] => 04月12日
            [username] => 刘宗源
            [birthday_interval] => 32
        )

    [9] => Array
        (
            [birthday_range] => 三月外
            [id] => 17
            [age] => 17
            [birthday] => 01月19日
            [username] => 不知道
            [birthday_interval] => 319
        )

)
</code>
登入後複製
登入後複製

回复内容:

按照birthday_interval升序排序

<code>Array
(
    [0] => Array
        (
            [birthday_range] => 三月外
            [id] => 8
            [age] => 24
            [birthday] => 12月04日
            [username] => haha
            [birthday_interval] => 273
        )

    [1] => Array
        (
            [birthday_range] => 三月外
            [id] => 9
            [age] => 24
            [birthday] => 12月04日
            [username] => haha
            [birthday_interval] => 283
        )

    [2] => Array
        (
            [birthday_range] => 三月内
            [id] => 10
            [age] => 16
            [birthday] => 04月12日
            [username] => 123
            [birthday_interval] => 37
        )

    [3] => Array
        (
            [birthday_range] => 三月内
            [id] => 11
            [age] => 16
            [birthday] => 04月12日
            [username] => xcvxcvxcv
            [birthday_interval] => 37
        )

    [4] => Array
        (
            [birthday_range] => 三月内
            [id] => 12
            [age] => 16
            [birthday] => 04月12日
            [username] => asasdfgh
            [birthday_interval] => 37
        )

    [5] => Array
        (
            [birthday_range] => 三月内
            [id] => 13
            [age] => 16
            [birthday] => 04月12日
            [username] => 1123
            [birthday_interval] => 32
        )

    [6] => Array
        (
            [birthday_range] => 三月内
            [id] => 14
            [age] => 16
            [birthday] => 04月12日
            [username] => 123
            [birthday_interval] => 31
        )

    [7] => Array
        (
            [birthday_range] => 三月内
            [id] => 15
            [age] => 16
            [birthday] => 04月12日
            [username] => 123
            [birthday_interval] => 37
        )

    [8] => Array
        (
            [birthday_range] => 三月内
            [id] => 16
            [age] => 21
            [birthday] => 04月12日
            [username] => 刘宗源
            [birthday_interval] => 32
        )

    [9] => Array
        (
            [birthday_range] => 三月外
            [id] => 17
            [age] => 17
            [birthday] => 01月19日
            [username] => 不知道
            [birthday_interval] => 319
        )

)
</code>
登入後複製
登入後複製

<code><?php usort( $users, function( $a, $b ) {
    return $a['birthday_interval'] - $b['birthday_interval'];
});
</code></code>
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板