我有以下json字符串,我想以A-Z格式排序。
{
"buses":
[
{
"name": "Shuttle 10: City 1 - City 5 (11 seats left)",
"id": 12
},
{
"name": "Shuttle 11: City 1 - City 5 (11 seats left)",
"id": 13
},
{
"name": "Shuttle 12: City 1 - City 5 (11 seats left)",
"id": 14
},
{
"name": "Shuttle 13: City 1 - City 5 (11 seats left)",
"id": 15
},
{
"name": "Shuttle 14: City 1 - City 5 (11 seats left)",
"id": 16
},
{
"name": "Shuttle 15: City 1 - City 5 (11 seats left)",
"id": 17
},
{
"name": "Shuttle 16: City 1 - City 5 (11 seats left)",
"id": 18
},
{
"name": "Shuttle 17: City 1 - City 5 (11 seats left)",
"id": 19
},
{
"name": "Shuttle 18: City 1 - City 5 (10 seats left)",
"id": 20
},
{
"name": "Shuttle 19: City 1 - City 5 (11 seats left)",
"id": 21
},
{
"name": "Shuttle 1: City 1 - City 5 (10 seats left)",
"id": 1
},
{
"name": "Shuttle 20: City 1 - City 5 (11 seats left)",
"id": 22
},
{
"name": "Shuttle 21: City 1 - City 5 (11 seats left)",
"id": 23
},
{
"name": "Shuttle 22: City 1 - City 5 (11 seats left)",
"id": 24
},
{
"name": "Shuttle 23: City 1 - City 5 (11 seats left)",
"id": 25
},
{
"name": "Shuttle 24: City 1 - City 5 (11 seats left)",
"id": 26
},
{
"name": "Shuttle 25: City 1 - City 5 (11 seats left)",
"id": 27
},
{
"name": "Shuttle 26: City 1 - City 5 (11 seats left)",
"id": 28
},
{
"name": "Shuttle 27: City 1 - City 5 (11 seats left)",
"id": 29
},
{
"name": "Shuttle 28: City 1 - City 5 (11 seats left)",
"id": 30
},
{
"name": "Shuttle 29: City 1 - City 5 (11 seats left)",
"id": 31
},
{
"name": "Shuttle 2: City 1 - City 5 (2 seats left)",
"id": 3
}
]
}
所以基本上我想按升序A-Z对字符串进行排序,结果首先出现这种情况:
{
"name": "Shuttle 1: City 1 - City 5 (10 seats left)",
"id": 1
}
这是最后一次。
{
"name": "Shuttle 29: City 1 - City 5 (11 seats left)",
"id": 31
}
我已将json字符串保存在变量$result
中,并且尝试sort函数不起作用。
$json = json_decode($result, true);
echo sort($json);
使用php中的sort并返回已排序的json字符串来实现此目的的最佳方法是什么?