Laravel 集合将数组转换为对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42182919/
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 15:18:41  来源:igfitidea点击:

Laravel collection converts array to object

phpjsonlaravellaravel-collection

提问by tgun926

If I run $collection->filter(myFilter), Laravel does this annoying thing of adding keys to each model in the collection like so:

如果我运行$collection->filter(myFilter),Laravel 会为集合中的每个模型添加键,就像这样:

{
    "4": {
        "myObject": "data"
    },
    "7": {
        "myObject": "data"
    }
}

How can I get rid of the "4" and "7" so it's an array of my objects?

如何摆脱“4”和“7”,使其成为我的对象数组?

My code that runs is:

我运行的代码是:

$obj = Cars::with('brand')->orderBy('id')->get();

return $obj->filter(function($value, $key)
{
    return $value->display == true;
});

回答by patricus

The issue is that the filter()method does not rekey the underlying collection array. So, the Collection is still representing an array, it is just that your array looks like this:

问题是该filter()方法不会重新生成底层集合数组的密钥。所以,集合仍然代表一个数组,只是你的数组看起来像这样:

[
    4 => Object4,
    7 => Object7,
]

While this is a perfectly valid array in PHP, this is not a proper array in JSON. Since this cannot be represented as an array in JSON, it is converted to an object in JSON.

虽然这在 PHP 中是一个完全有效的数组,但在 JSON 中这不是一个正确的数组。由于这不能在 JSON 中表示为数组,因此将其转换为 JSON 中的对象。

In order to get this properly represented as an array in JSON, you just need to rekey the Collection array. The proper method for this is the values()method. All it does is call array_valueson the underlying array. This will turn the above array in this:

为了将其正确表示为 JSON 中的数组,您只需要重新设置 Collection 数组的密钥。正确的方法是values()方法。它所做的只是调用array_values底层数组。这将把上面的数组变成这样:

[
    0 => Object4,
    1 => Object7,
]

Now, this is a proper numerically indexed array that JSON can understand and will treat as an array instead of an object.

现在,这是一个正确的数字索引数组,JSON 可以理解并将其视为数组而不是对象。

While flattenmay work for this particular case (your Collection is a collection of Eloquent Models), it is not actually the correct method, and may lead to unintended consequences. Additionally, it will perform a lot of extra logic that is not needed. Your best bet is to use the proper method for what you are trying to achieve, and that is the values()method.

虽然flatten可能适用于这种特殊情况(您的 Collection 是 Eloquent Models 的集合),但它实际上并不是正确的方法,并且可能会导致意想不到的后果。此外,它将执行许多不需要的额外逻辑。您最好的选择是使用正确的方法来实现您想要实现的目标,这就是values()方法。

$obj = Cars::with('brand')->orderBy('id')->get();

return $obj->filter(function($value, $key)
    {
        return $value->display == true;
    })
    ->values();

回答by Dwight

Calling flatten()on your collection should remove the keys and merge all their values up into a single collection.

调用flatten()您的集合应该删除键并将它们的所有值合并到一个集合中。