如何对以下数组进行排序? [英] How do I sort the following array?

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

问题描述

我有这个数组:

 数组([0] =>大批([id] =>6347[纬度] =>18.520430[lng] =>73.856743[country_id] =>18[说明] =>浦那是各种各样的好地方!!!!!!!!!!!!<br>[loc_badge] =>img/icon-monument.png[距离] =>0[国家] =>印度[城市] =>浦那[refId] =>6340[平均汇率] =>4.7)[1] =>大批([id] =>6587[纬度] =>18.649632[lng] =>73.744843[country_id] =>18[说明] =>Ravet 是一个不错的城市,靠近浦那<br>[loc_badge] =>img/icon-attraction.png[距离] =>18.5865663140631[国家] =>印度[城市] =>雷夫特[refId] =>6749[平均汇率] =>0)[2] =>大批([id] =>6633[纬度] =>17.691401[lng] =>74.000938[country_id] =>18[说明] =>satara 是各种不错的城市<br>[loc_badge] =>img/icon-monument.png[距离] =>93.4305849434119[国家] =>印度[城市] =>萨塔拉[refId] =>6834[平均汇率] =>6)[3] =>大批([id] =>6213[纬度] =>18.655491[lng] =>72.867920[country_id] =>150[说明] =>阿里巴格是印度马哈拉施特拉邦康坎地区莱加德区的一个沿海城镇和市政委员会.[loc_badge] =>img/icon-beach.png[距离] =>105.287803206408[国家] =>马哈拉施特拉邦[城市] =>阿里巴巴[refId] =>6212[平均汇率] =>2))

我想按照 avgRate 的降序对这个数组进行排序,这样数组索引就会是这样的:

2 0 3 1

知道如何做到这一点吗?

解决方案

试试这个:使用 array_multisort

$sort = array();foreach($your_array as $k=>$v) {$sort['avgRate'][$k] = $v['avgRate'];}array_multisort($sort['avgRate'], SORT_DESC, $your_array);echo "

";print_r($your_array);

参考:http://php.net/manual/en/function.数组-multisort.php

I have this array:

  Array
(
[0] => Array
    (
        [id] => 6347
        [lat] => 18.520430
        [lng] => 73.856743
        [country_id] => 18
        [description] => pune is vary nice plac!!!!!!!!!!!!<br>
        [loc_badge] => img/icon-monument.png
        [distance] => 0
        [country] => india
        [city] => pune
        [refId] => 6340
        [avgRate] => 4.7
    )

[1] => Array
    (
        [id] => 6587
        [lat] => 18.649632
        [lng] => 73.744843
        [country_id] => 18
        [description] => Ravet is vary nice city near by pune<br>
        [loc_badge] => img/icon-attraction.png
        [distance] => 18.5865663140631
        [country] => india
        [city] => ravet
        [refId] => 6749
        [avgRate] => 0
    )

[2] => Array
    (
        [id] => 6633
        [lat] => 17.691401
        [lng] => 74.000938
        [country_id] => 18
        [description] => satara is vary nice city<br>
        [loc_badge] => img/icon-monument.png
        [distance] => 93.4305849434119
        [country] => india
        [city] => satara
        [refId] => 6834
        [avgRate] => 6
    )

[3] => Array
    (
        [id] => 6213
        [lat] => 18.655491
        [lng] => 72.867920
        [country_id] => 150
        [description] => Alibag  is a coastal town and a municipal council in Raigad District in the Konkan region of Maharashtra, India.
        [loc_badge] => img/icon-beach.png
        [distance] => 105.287803206408
        [country] => maharashtra
        [city] => alibag
        [refId] => 6212
        [avgRate] => 2
    ) 
)

I want to sort this array in descending order of avgRate so that the array index will be like this:

2 0 3  1

any idea how can this be done?

解决方案

Try this : Using array_multisort

$sort = array();
foreach($your_array as $k=>$v) {
    $sort['avgRate'][$k] = $v['avgRate'];
}

array_multisort($sort['avgRate'], SORT_DESC, $your_array);


echo "<pre>";
print_r($your_array);

Ref: http://php.net/manual/en/function.array-multisort.php

这篇关于如何对以下数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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