PHP带键值的排序数组 [英] PHP Sort array with key value

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

问题描述

我要用键值对数组进行排序.使用以下数组,我需要按 site_category 对其进行排序,并且需要具有类似

I'm breaking my head to sort array with key value. With this following array i need to sort this by site_category and i need to have output like

示例:

Shopping
   Amazon
Social
   Amoeblo
   American express

数组值是

(
    [0] => stdClass Object
        (
            [site_id] => 1
            [site_name] => Amazon
            [site_img] => http://localhost/faves/resource/img/sites/icon/amazon.png
            [site_category] => Shopping
        )

    [1] => stdClass Object
        (
            [site_id] => 2
            [site_name] => Ameblo
            [site_img] => http://localhost/faves/resource/img/sites/icon/ameblow.png
            [site_category] => Social
        )

    [2] => stdClass Object
        (
            [site_id] => 3
            [site_name] => American Express
            [site_img] => http://localhost/faves/resource/img/sites/icon/americanexpress.png
            [site_category] => Social
        )


)

有什么解决建议吗?

推荐答案

您可以使用foreach和ksort来实现

You can do it using a foreach and ksort

让$ your_array为您上面提到的数组

Let $your_array be the array you mentioned above

$res_array    = array();
foreach($your_array as $val){
   $res_array[$val->site_category][] = $val->site_name;
}
ksort($res_array);

print_r($res_array);

或在php中搜索多排序,将解决您的问题:)

OR search for multisort in php which will solve your problem :)

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

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