无法使用PHP

时间:2016-09-13 05:58:13

标签: php sorting

我需要使用PHP以asc顺序对数组值进行排序。我在下面解释我的代码。

$data=[
  {"restaurant_name":"The Garage Sports Bar","distance":"0.48Km"},
  {"restaurant_name":"A&P Chinese Food Express","distance":"8.81Km"},
  {"restaurant_name":"Green Chilli","distance":"19.37Km"},
  {"restaurant_name":"Pulcinella - Authentic Napoletana Pizza","distance":"1.31Km"}
]

我有以上数据按asc顺序排序我的PHP代码如下所示。

$sortArray = array();
foreach($data as $person){
    foreach($person as $key=>$value){
if(!isset($sortArray[$key])){
 $sortArray[$key] = array(); 
}
$sortArray[$key][] = $value;
    }
}
$orderby = "distance";
array_multisort($sortArray[$orderby],SORT_ASC,$data);

排序后,我得到以下输出。

$data=[{"restaurant_name":"The Garage Sports Bar","distance":"0.48Km"},{"restaurant_name":"Pulcinella - Authentic Napoletana Pizza","distance":"1.31Km"},{"restaurant_name":"Green Chilli","distance":"19.37Km"},{"restaurant_name":"A&P Chinese Food Express","distance":"8.81Km"}]

但它无法按照ASC顺序排序。

1 个答案:

答案 0 :(得分:1)

SORT_NUMERIC函数中将排序类型指定为array_multisort,如下所示

array_multisort($sortArray[$orderby],SORT_ASC,SORT_NUMERIC,$dataA);

这将为您提供所需的结果。