Laravel 分组集合返回对象而不是数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51087064/
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
Laravel grouped collection returns object instead of array
提问by Kyle Reierson
I have the following query
我有以下查询
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
});
return response()->json([
'outings' => $outings
], 200);
The response is returning an object and I need it to return an array
How can I get outings to be an array instead of an object.
我怎样才能让郊游成为一个数组而不是一个对象。
If I don't group the collection and just do
如果我不将集合分组而只是这样做
Outing::all();
It will return an array rather than an object. The Group by is doing something weird.
它将返回一个数组而不是一个对象。Group by 正在做一些奇怪的事情。
If I DD($outings) it does in fact return a collection, so I think it's odd that it gets cast to an object when returned to the browser rather than an array.
如果我 DD($outings) 它实际上返回一个集合,所以我认为它在返回到浏览器而不是数组时被转换为一个对象是很奇怪的。
Below is the output when I DD($outings->toArray())
下面是我 DD($outings->toArray()) 时的输出
Thanks
谢谢
采纳答案by rkj
If you want array then use this
如果你想要数组然后使用这个
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
})->map(function($item){
return $item->all();
});
return response()->json($outings, 200);
If you want date as key then
如果你想要日期作为关键,那么
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
});
return response()->json($outings->toArray(), 200);
回答by Turbotailz
Use array_values($array)
to force an associative array into a JSON array. You will lose any key names this way, however.
用于array_values($array)
将关联数组强制转换为 JSON 数组。但是,您会以这种方式丢失任何键名。
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
});
return response()->json([
'outings' => array_values($outings)
], 200);
No one has really explained why this happens.
没有人真正解释过为什么会发生这种情况。
In JavaScript/JSON, arrays are just collections of values, they are keyed but the key is always numerical in sequence.
在 JavaScript/JSON 中,数组只是值的集合,它们是有键的,但键总是按顺序排列的数字。
In PHP, arrays are collections of "key" => "value"
pairs, the key can be anything (integer or string) but arrays can generally be considered "associative" (where the key is non-numerical, or numerical but not in sequence) or "non-associative" (where the key is numerical AND in sequence (similar to JS arrays).
在 PHP 中,数组是"key" => "value"
成对的集合,键可以是任何东西(整数或字符串),但数组通常可以被认为是“关联的”(其中键是非数字的,或数字但不按顺序)或“非关联的” (其中键是数字 AND 顺序(类似于 JS 数组)。
When it comes to encoding a PHP array as JSON, say an array has keys that are non-numeric OR the keys are numeric but not in sequential order (e.g. 0, 1, 2, 4, 5, 8, 10) - these types of arrays are not compatible with JavaScript/JSON arrays. In this case, the array is converted into an object to preserve the keys. To force the array to be converted into an array, you must convert the array into a non-associative array (numerical sequenced keys), PHP has the function array_values
to help with this.
在将 PHP 数组编码为 JSON 时,假设数组具有非数字键或键是数字但不是按顺序排列的(例如 0、1、2、4、5、8、10)——这些类型数组与 JavaScript/JSON 数组不兼容。在这种情况下,数组被转换为一个对象以保留键。要强制将数组转换为数组,必须将数组转换为非关联数组(按数字顺序排列的键),PHP 有功能array_values
可以帮助完成此操作。
回答by user3574492
Try doing:
尝试做:
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
});
return response()->json([
'outings' => $outings->toArray()
], 200);
回答by Adnan Mumtaz
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
})->toArray();
return response()->json([
'outings' => $outings
], 200);
Have a look at toArray()
Also Try
也试试
$outings = Outing::all()->groupBy(function ($item) {
return Carbon::parse($item['start'])->format('m/d/Y');
})->toArray();
return response()->json([
'outings' => json_encode($outings)
], 200);