【发布时间】:2014-05-13 02:11:09
【问题描述】:
我的 JSON 字符串,
$rs 是我的数组,
我正在做,
while($ra[] = mysql_fetch_assoc($dno))
{
$restaurants['id']=$ra[$counter]['R_ID'];
$restaurants['name']=$ra[$counter]['name'];
$distancefull = distance($userlat, $userlon, $ra[$counter]['lat'], $ra[$counter]['lng'], "K");
$restaurants['distance'] = (float)round($distancefull, 2);
$restaurants['contact']=$ra[$counter]['contact'];
$restaurants['area']=$ra[$counter]['area'];
$counter++;
$rs[]=$restaurants;
}
function cmp($a, $b)
{
//return strcmp($a["distance"], $b["distance"]);
return ($a["distance"] - $b["distance"]);
}
usort($rs, "cmp");
return $rs;
那么,
json_encode(array('Reataurants'=>$rs),JSON_NUMERIC_CHECK);
我想按距离对对象进行排序,但只完成了前两个。
感谢您的帮助。
【问题讨论】:
-
$rs是直接从json_decode()得到的结果吗?或者您是否将$rs设置为json_decode()调用返回的对象的Restaurants属性处的数组?换句话说,你做了$rs = json_decode($json);还是$json_object = json_decode($json); $rs = $json_object->Restaurants;? -
json_encode(array('Reataurants'=>$rs),JSON_NUMERIC_CHECK);
-
这毫无意义。您需要解码 JSON 才能使用它。
-
usort之前的$rs是什么,之后是什么?
-
我想在 json_encode 之前对数组进行排序,所以可能不需要解码,请参阅评论问题。我添加了更多细节。