一尘不染

按特定键对多维数组排序

php

我有一个数组:

Array (
    [0] => stdClass Object (
        [user_id] => 1
        [ID] => 1
        [user_login] => admin
        [display_name] => admin
        [user_email] => webbinformation@nacka.se
        [meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
    )
    [1] => stdClass Object (
        [user_id] => 4
        [ID] => 4
        [user_login] => ungtinflytande
        [display_name] => ungtinflytande
        [user_email] => klarakviberg@gmail.com
        [meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
    )
    [2] => stdClass Object (
        [user_id] => 5
        [ID] => 5
        [user_login] => inflytandepilot
        [display_name] => inflytandepilot
        [user_email] => hildalundgren@hotmail.com
        [meta_value] => a:1:{s:6:\"author\";s:1:\"1\";}
    )
    [3] => stdClass Object (
        [user_id] => 11
        [ID] => 11
        [user_login] => matsbohman
        [display_name] => matsbohman
        [user_email] => mats.bohman@nacka.se
        [meta_value] => a:1:{s:6:\"editor\";s:1:\"1\";}
    )
    [4] => stdClass Object (
        [user_id] => 12
        [ID] => 12
        [user_login] => klarakviberg
        [display_name] => klarakviberg
        [user_email] => nastasteg@nacka.se
        [meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
    )
)

…我想按display_name钥匙排序 我目前这样打印:

foreach ($blogusers as $bloguser) {
    ...
}

我该怎么做呢?


阅读 262

收藏
2020-05-29

共1个答案

一尘不染

您将使用usort()-http: //php.net/usort

我的建议是:

    function cmp($a, $b)
    {
        return strcmp($a->display_name, $b->display_name);
    }

    usort($blogusers, "cmp");

    foreach ($blogusers as $bloguser)
    {
        ...
2020-05-29