Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用PHP按键按asc顺序对值进行排序_Php_Sorting - Fatal编程技术网

无法使用PHP按键按asc顺序对值进行排序

无法使用PHP按键按asc顺序对值进行排序,php,sorting,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"}, {"restaur

我需要使用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顺序正确排序。

数组\u多排序
函数中,将排序类型指定为
排序\u数值
,如下所示

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

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

发布正确的$data数组值