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

Ale*_*ngt 4 javascript json laravel

这是我的代码:

    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';
            })),

        ]);
    }
Run Code Online (Sandbox Code Playgroud)

这是回应:

{
  "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"
        }
      }
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

Tim*_*wis 7

在内部array_filter()过滤数组中的匹配条目,并以完整的索引返回它们。object这非常重要,因为这是您得到 an而不是arrayin的核心原因JavaScript

在 中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)
]);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,类具有类似的逻辑,我更喜欢尽可能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()
]);
Run Code Online (Sandbox Code Playgroud)

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

参考:

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

  • @Adam 不用担心:) 这是一个冗长的解释,绝对很容易错过。干杯! (2认同)