我知道這個問題并不新鮮,但我沒有找到解決方案。我有一個返回 JSON 的 API 請求:$values = '{"retcode" : "0 Done", "trans_id" : "326678", "answer" : [ { "City" : "New York", "Digits" : "5", "Datetime" : "1590773203", "DatetimeMsc" : "1590773203568", "Value" : "0.66480", "Average" : "0.66485", "Last" : "0.00000", "Volume" : "0", "VolumeReal" : "0" } , { "City" : "Berlin","Digits" : "5", "Datetime" : "1590773203", "DatetimeMsc" : "1590773203568", "Value" : "0.89917", "Average" : "0.89920", "Last" : "0.00000", "Volume" : "0", "VolumeReal" : "0" } , { "City" : "Paris", "Digits" : "3", "Datetime" : "1590773203", "DatetimeMsc" : "1590773203569", "Value" : "119.680", "Average" : "119.684", "Last" : "0.000", "Volume" : "0", "VolumeReal" : "0" } , { "City" : "Athens", "Digits" : "5", "Datetime" : "1590773203", "DatetimeMsc" : "1590773203569", "Value" : "1.11211", "Average" : "1.11213", "Last" : "0.00000", "Volume" : "0", "VolumeReal" : "0" } , { "City" : "Beijing", "Digits" : "5", "Datetime" : "1590773203", "DatetimeMsc" : "1590773203568", "Value" : "1.23677", "Average" : "1.23686", "Last" : "0.00000", "Volume" : "0", "VolumeReal" : "0" 我將 JSON 轉(zhuǎn)換為一個數(shù)組,并創(chuàng)建一個自定義順序取決于城市的數(shù)組此代碼返回與 json_decode 之后相同的數(shù)組。有什么建議么?提前致謝。
1 回答

ITMISS
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個贊
只需在 usort 函數(shù)中將數(shù)組 $values 更改為 $values['answer']
$values=json_decode($values, true);
$order = array('Rome', 'Berlin', 'Tokyo','Beijing',
'New York','Athens','Moscow','Los Angeles',
'Paris','Madrid','Chicago','Melbourne','Amman');
usort($values['answer'], function ($a, $b) use ($order) {
$pos_a = array_search($a['City'], $order);
$pos_b = array_search($b['City'], $order);
return $pos_a - $pos_b;
});
- 1 回答
- 0 關(guān)注
- 109 瀏覽
添加回答
舉報(bào)
0/150
提交
取消