{ "result": true,
"users": [{
"id": 11,
"expected_count": 13,
"user_id": 1,
"event_id": 2,
"user": {
"id": 1,
"name": "Moiz Jamali"
}
},
{
"id": 12,
"expected_count": 12,
"user_id": 2,
"event_id": 2,
"user": {
"id": 2,
"name": "Juzer Samiwala"
}
}]
}
在我的Big Array的输出之上('用户')。
我想要做的是根据PHP中小数组(' user')内的名称值对这个大数组(' users')进行排序。
有人可以帮我解决这个问题吗?
谢谢。
答案 0 :(得分:0)
$bigArray = ["result" => true, "users" => [
"id" => 11,
"expected_count" => 13,
"user_id" => 1,
"event_id" => 2,
"user" => [
"id" => 1,
"name" => "Moiz Jamali"
],
... other users
]];
usort($bigArray['users'], function ($a, $b)
{
return strcmp($a['user']['name'], $b['user']['name']);
});
答案 1 :(得分:0)
您必须使用自己的函数usort()来对数组进行排序
解决方案:
<?php
$data = json_decode('{ "result": true, "users": [{ "id": 11, "expected_count": 13, "user_id": 1, "event_id": 2, "user": { "id": 1, "name": "Moiz Jamali" } }, { "id": 12, "expected_count": 12, "user_id": 2, "event_id": 2, "user": { "id": 2, "name": "Juzer Samiwala" } }] }');
function cmp($a, $b)
{
if ($a->user->name == $b->user->name) {
return 0;
}
return ($a->user->name < $b->user->name) ? -1 : 1;
}
usort($data->users, "cmp");
print_r($data);