duanfu5239
2013-10-06 14:41
浏览 40
已采纳

按特定值在PHP中对数组进行排序

How can i sort this array by $data['response']['games'][x]['name'] alphabetical from A-Z?

I have tried already array_multisort() but didn't understand this function at all.

Hope you can help me - googled and searched this but didn't found any solution for my problem.

Edit: link updated.

Code: https://github.com/GabrielWanzek/GWSteamLib/

图片转代码服务由CSDN问答提供 功能建议

如何排序 $ data ['response'] ['games'] [x] ['name'] 按字母顺序排列“nofollow”>这个 数组?

我已经尝试过 array_multisort()但根本不理解这个功能。

希望你能帮助我 - 谷歌搜索 搜索了这个,但没有找到解决我问题的方法。

编辑:链接已更新。

代码: < a href =“https://github.com/GabrielWanzek/GWSteamLib/”rel =“nofollow”> https://github.com/GabrielWanzek/GWSteamLib/

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanluan8390 2013-10-06 14:46
    已采纳

    You can achieve this with usort(), which allows you to define a custom comparison function:

    usort($data['response']['games'], function($a, $b) {
        return strcmp($a['name'], $b['name']);
    });
    

    Note that $data is an object of type stdClass; it is not an array.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhuan1380 2013-10-06 15:04

    Try following code:

    $games = $data['response']['games']; // is array
    usort($games, 'compareName');
    var_dump($games);
    
    # want to change $data?
    # $data['response']['games'] = $games;
    
    function compareName($a1, $a2) {
        return strcmp($a1['name'], $a2['name']);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题