将 Laravel 集合中的数组数组转换为带有 json 数组的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36849481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:40:54  来源:igfitidea点击:

Converting an array of arrays from Laravel's collection into an object with an array in json

phpjsonlaravelfunctional-programminglaravel-5.2

提问by Chris Schmitz

I am generating an an array of categories and subcategories in my Laravel API for use in my client side app.

我正在我的 Laravel API 中生成一系列类别和子类别,以便在我的客户端应用程序中使用。

I'm doing this by using the collection's filterand mapmethods to organize the data pulled from the database:

我这样做是通过使用集合filtermap方法来组织从数据库中提取的数据:

// get the data from the category table
$categoryData = $categories->all()->categories();

// filter the data by category names we know we want and create the multi-dimensional array of the fields we need
$categoryList = $categoryData->filter(function ($value) {
    return $value['Category'] == "by Area of Interest" || $value['Category'] == "by Grade Level";
})
->map(function ($value) {
    return ['category' => $value['Category'], 'subCategory' => $value['Subcategory']];
});

dd($categoryList);

When I review the die and dump result, I get my collection with it's array of correctly formatted items:

当我查看 die 和 dump 结果时,我得到了我的集合,其中包含格式正确的项目数组:

dd result

结果

And if I output it as an array with:

如果我将它输出为一个数组:

dd($categoryList->toArray());

I see the multi-dimensional array I would expect.

我看到了我期望的多维数组。

dd result when outputting as an array

dd 输出为数组时的结果

The problem I'm running into is when I try to output it as json for my client app to consume.

我遇到的问题是当我尝试将它输出为 json 以供我的客户端应用程序使用时。

dd($categoryList->toJson());

dd($categoryList->toJson());

If I output as json I get an object that contains the php array indexes as properties and the php sub arrays as objects assigned to those properties.

如果我输出为 json,我会得到一个对象,其中包含 php 数组索引作为属性和 php 子数组作为分配给这些属性的对象。

dd result when outputting to json

输出到json时的dd结果

What I want is a json object that contains an array of objects. e.g.

我想要的是一个包含对象数组的 json 对象。例如

$categoryList = [['category' => 'test', 'subCategory' => 'test'], ['category' => 'test', 'subCategory' => 'test']];
dd(json_encode($categoryList));

Which outputs:

哪些输出:

example of what I want

我想要的例子

Is there something I'm doing wrong with my collection calls?

我的收款电话有什么问题吗?

Update

更新

Per Hognbin's comment:

根据 Hognbin 的评论:

The call to $categories->all()->categories();returns a collection of category data that I then manipulate to get my list.

调用$categories->all()->categories();返回一个类别数据的集合,然后我操作这些数据来获取我的列表。

$categoryData = $categories->all()->categories();
dd($categoryData);

yields:

产量:

result of categories->all()

类别的结果->全部()

采纳答案by Filip Koblański

The problem is that the indexes are the ids of categories which You'll get from eloquent collection when You try to make array from it.

问题是索引是类别的 id,当您尝试从中创建数组时,您将从 eloquent 集合中获得这些 id。

If you want to have a clean array of objects, I suggest doing this:

如果你想要一个干净的对象数组,我建议这样做:

json_encode(array_values($categoryList->toArray()));

Final implementation by Chris

克里斯的最终实现

The above explanation and function calls got me the end result I needed. After confirming it worked via these function calls I looked at the Collectionclass methods to see how I could implement it using the class. Here's what I did:

上面的解释和函数调用让我得到了我需要的最终结果。在通过这些函数调用确认它可以工作后,我查看了Collection类方法以了解如何使用该类来实现它。这是我所做的:

$categoryList = $categoryData->filter(function ($value) {
    return $value['Category'] == "by Area of Interest" || $value['Category'] == "by Grade Level";
})
->map(function ($value) {
    return ['category' => $value['Category'], 'subCategory' => $value['Subcategory']];
})
->values()
;

If you look at the Collectionclass' values()method, it's doing the same thing:

如果您查看Collection类的values()方法,它会做同样的事情:

values method in Collection class

Collection 类中的 values 方法

Since the Laravel Collection itemsare stored as an array, the toArray()method call is not needed.

由于 Laravel 集合items存储为数组,因此toArray()不需要方法调用。