gpt4 book ai didi

php - 如何在 laravel 中对 json 对象进行排序

转载 作者:行者123 更新时间:2023-12-02 15:00:07 29 4
gpt4 key购买 nike

我有以下响应,如何根据距离对其进行排序

{
"Message": "Done.",
"Status": true,
"InnerData": [
{
"id": 66,
"name": "tito",
"distance": 74,
},
{
"id": 67,
"name": "liver pool",
"distance": 83
},
{
"id": 67,
"name": "Text",
"distance": 72
}
]
}

我尝试了 usort 但我没有成功。我也试过这个答案here但是好像和我需要的不一样

最佳答案

在纯 PHP 7 中

<?php

$json = '{
"Message": "Done.",
"Status": true,
"InnerData": [
{
"id": 66,
"name": "tito",
"distance": 74
},
{
"id": 67,
"name": "liver pool",
"distance": 83
},
{
"id": 67,
"name": "Text",
"distance": 72
}
]
}';
$array = json_decode($json, true);
usort($array['InnerData'], function($a, $b) {
return $a['distance'] <=> $b['distance'];
});

print_r($array);

关于php - 如何在 laravel 中对 json 对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50450667/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com