gpt4 book ai didi

javascript - Laravel - 为什么json响应有时返回一个数组有时返回一个对象

转载 作者:行者123 更新时间:2023-12-04 00:59:34 25 4
gpt4 key购买 nike

这是我的代码:

    public function list()
{
$users = User::with('group')->get()->toArray();
return response()->json([

'clients' => array_filter($users, function ($r) {
return $r['group']['name'] === 'client';
}),

'employes' => array(array_filter($users, function ($r) {
return $r['group']['name'] !== 'client';
})),

]);
}

这是回应:
{
"clients": {
"2": {
"id": 3,
"name": "Client 1",
"email": "client@a.fr",
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"group_id": 4,
"group": {
"id": 4,
"name": "client"
}
},
"3": {
"id": 4,
"name": "Client 2",
"email": "client2@a.fr",
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"group_id": 4,
"group": {
"id": 4,
"name": "client"
}
},
"4": {
"id": 5,
"name": "Client 3",
"email": "client3@a.fr",
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"group_id": 4,
"group": {
"id": 4,
"name": "client"
}
}
},
"employes": [
[
{
"id": 1,
"name": "Alexis",
"email": "alexis@a.fr",
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"group_id": 1,
"group": {
"id": 1,
"name": "admin"
}
},
{
"id": 2,
"name": "guest",
"email": "guest@a.fr",
"email_verified_at": null,
"created_at": null,
"updated_at": null,
"group_id": 2,
"group": {
"id": 2,
"name": "guest"
}
}
]
]
}

我试图改变 array_filter 的条件.有时我有一个数组,有时我有一个对象。我不明白这是如何确定的

Stackoverflow 告诉我“看起来你的帖子主要是代码;请添加更多细节。”那么......要添加哪些细节?

谢谢

最佳答案

内部 array_filter()从数组中过滤匹配的条目,并返回它们他们的指数完好无损 .这非常重要,因为这是您获得 object 的核心原因。而不是 arrayJavaScript .

PHP , 这可以;数组可以从 0 或其他索引开始,例如 2 ,并且可以毫无问题地用作数组。这是由于indexed的存在(从 0 开始)和 associative (基于数字/非数字键的)数组。

JavaScript ,数组不能是“关联的”,即它们必须从 0 开始。object另一方面,类的功能类似于关联数组,但主要区别在于它们不是显式数组。

现在您知道原因了,下一个问题是“我该如何解决这个问题?”简单的方法是换行array_filter()使用一个简单地返回新数组的值的函数。这本质上将使用基于 0 的值“重新索引”数组,当转换为 JSON 时将导致返回正确的数组:

$users = User::with('group')->get()->toArray();

$clients = array_filter($users, function($r){
return $r['group']['name'] === 'client';
});

$groups = array_filter($users, function ($r) {
return $r['group']['name'] !== 'client';
});

return response()->json([
'clients' => array_values($clients),
'groups' => array_values($groups)
]);

作为旁注, Collection类有类似的逻辑,我更喜欢使用 Laravel 的 Collection尽可能上课:
$users = User::with('group')->get();

$clients = $users->filter(function($r){
$r->group->name === 'client';
});

$groups = $users->filter(function($r){
$r->group->name !== 'client';
});

return response()->json([
'clients' => $clients->values(),
'groups' => $groups->values()
]);

但同样,这是我的偏好;任何一种方法都应该有效,因此请使用您习惯的方法。

引用:
PHP数组方法:

https://www.php.net/manual/en/function.array-filter.php

https://www.php.net/manual/en/function.array-values.php

Laravel 收集方法:

https://laravel.com/docs/5.8/collections#method-filter

https://laravel.com/docs/5.8/collections#method-values

关于javascript - Laravel - 为什么json响应有时返回一个数组有时返回一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685133/

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