将Laravel集合中的数组数组转换为带有json中数组的对象
我在Laravel API中生成了一系列类别和子类别,供我的客户端应用程序使用.
I am generating an an array of categories and subcategories in my Laravel API for use in my client side app.
我这样做是通过使用集合的 filter
和 map
方法来组织从数据库中提取的数据:
I'm doing this by using the collection's filter
and map
methods to organize the data pulled from the database:
// 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:
如果我将其输出为数组,则使用:
And if I output it as an array with:
dd($categoryList->toArray());
我看到了我期望的多维数组.
I see the multi-dimensional array I would expect.
我遇到的问题是当我尝试将其输出为json以便客户端应用程序使用时.
The problem I'm running into is when I try to output it as json for my client app to consume.
dd($ categoryList-> toJson());
dd($categoryList->toJson());
如果我输出为json,则会得到一个对象,该对象包含php数组索引作为属性,而php子数组作为分配给这些属性的对象.
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对象.例如
What I want is a json object that contains an array of objects. e.g.
$categoryList = [['category' => 'test', 'subCategory' => 'test'], ['category' => 'test', 'subCategory' => 'test']];
dd(json_encode($categoryList));
哪个输出:
我的催收电话有问题吗?
Is there something I'm doing wrong with my collection calls?
每个霍格宾的评论:
对 $ categories-> all()-> categories();
的调用将返回类别数据的集合,然后我将其操纵以获取列表.
The call to $categories->all()->categories();
returns a collection of category data that I then manipulate to get my list.
$categoryData = $categories->all()->categories();
dd($categoryData);
产量:
问题在于,索引是您尝试从数组中获取数组时从雄辩的集合中获取的类别的ID.
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.
如果您想要一个干净的对象数组,我建议您这样做:
If you want to have a clean array of objects, I suggest doing this:
json_encode(array_values($categoryList->toArray()));
克里斯的最终实施
上面的解释和函数调用使我得到了所需的最终结果.通过这些函数调用确认它可以正常工作后,我查看了 Collection
类方法,以了解如何使用该类实现它.这是我所做的:
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 Collection
class methods to see how I could implement it using the class. Here's what I did:
$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()
;
如果您查看 Collection
类的 values()
方法,则它的作用相同:
If you look at the Collection
class' values()
method, it's doing the same thing:
由于Laravel集合 items
作为数组存储,因此不需要 toArray()
方法调用.
Since the Laravel Collection items
are stored as an array, the toArray()
method call is not needed.