我正在使用PHP CodeIgniter框架。这里我有一个带有值的数组,距离字段在所有数组中。所以我想根据距离变量对这个数组进行排序。当距离很小时,它应该显示在数组的顶部。
Array
(
[3] => Array
(
[distance] => 0.1867701321678877
[search_name] => gshshs
[type] => vendor
)
[4] => Array
(
[distance] => 0.2581692930636528
[search_name] => imarahtech
[type] => vendor
)
[5] => Array
(
[distance] => 13.022316349008124
[delivery_available] => Yes
[search_name] => mius
[type] => vendor
)
[6] => Array
(
[distance] => 23.49767368340837
[search_name] => imarahtech
[type] => vendor
)
[7] => Array
(
[distance] => 37.85888922426821
[search_name] => blu
[type] => vendor
)
[8] => Array
(
[distance] => 0.18766008198496642
[search_name] => fried rice
[type] => product
)
[14] => Array
(
[distance] => 13.022316349008124
[search_name] => meals rice
[type] => product
)
[15] => Array
(
[distance] => 37.85888922426821
[search_name] => ayla
[type] => product
)
)
所以这就是我想要的,显示数据升序。
输出
Array
(
[3] => Array
(
[distance] => 0.1867701321678877
[search_name] => gshshs
[type] => vendor
)
[8] => Array
(
[distance] => 0.18766008198496642
[search_name] => fried rice
[type] => product
)
[4] => Array
(
[distance] => 0.2581692930636528
[search_name] => imarahtech
[type] => vendor
)
[5] => Array
(
[distance] => 13.022316349008124
[delivery_available] => Yes
[search_name] => mius
[type] => vendor
)
[14] => Array
(
[distance] => 13.022316349008124
[search_name] => meals rice
[type] => product
)
[6] => Array
(
[distance] => 23.49767368340837
[search_name] => imarahtech
[type] => vendor
)
[7] => Array
(
[distance] => 37.85888922426821
[search_name] => blu
[type] => vendor
)
[15] => Array
(
[distance] => 37.85888922426821
[search_name] => ayla
[type] => product
)
)
有人请帮我解决这个问题。
感谢你
答案 0 :(得分:1)
试试这个
usort($arrData, function($a, $b)
{
if ($a['distance'] == $b['distance']) return 0;
return ($a['distance'] > $b['distance']) ? 1 : -1;
});
这是有据可查的here
答案 1 :(得分:0)
在codeigniter中,您可以通过将行添加到模型
轻松地在SQL中执行此操作public function yourMethod(){
$this->db->select();
$this->db->from("your_table");
//$this->db->where(...); //some condition, I guess
$this->db->order_by('distance', "ASC");
$query = $this->db->get();
return $query->result();
}