laravel 从多维数组创建集合?

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

Creating a collection from a multidimensional array?

laravellaravel-5laravel-5.4

提问by panthro

I'm trying to make a collection from some arrays of data:

我正在尝试从一些数据数组中收集:

$myCollection = collect(
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
);

When I loop out I would like to do:

当我退出时,我想做:

foreach ($myCollection as $product) {
    echo $product->price;
    echo $product->discount;
}

But the underlying elements appear to still be in an arrays format, how can I achieve the above output?

但是底层元素似乎仍然是数组格式,我该如何实现上述输出?

回答by Gravy

If you want the inner arrays to be a collection, then you can do so as follows:

如果你想让内部数组成为一个集合,那么你可以这样做:

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
])->map(function($row) {
    return collect($row);
});

If you want the inner arrays to be objects, then you can do as follows:

如果您希望内部数组是对象,那么您可以执行以下操作:

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
])->map(function($row) {
    return (object) $row;
});

You can also iterate over each of the results...

您还可以迭代每个结果...

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
])->map(function($row) {
    return (object) $row;
})->each(function($row) {
    echo sprintf('ProductId: %d, Price: %d, Discount: %s'.PHP_EOL, $row->product_id, $row->price, $row->discount);
});

Output:

输出:

ProductId: 1, Price: 200, Discount: 50
ProductId: 2, Price: 400, Discount: 50

回答by Troyer

Simple as you are getting a collection of an associative arrays because you are collecting arrays elements, so the collection helper doesn't want to modify it in case you need it as array.

很简单,因为您正在收集关联数组的集合,因为您正在收集数组元素,因此集合助手不想修改它,以防您需要它作为数组。

Knowing this, if you want to collect objects you should pass an element of object type.

知道这一点,如果你想收集对象,你应该传递一个对象类型的元素。

You can cast object data type into the array, like this:

您可以将对象数据类型转换为数组,如下所示:

$myCollection = collect( (object) array(
     (object)  ['product_id' => 1, 'price' => 200, 'discount' => '50'],
     (object)  ['product_id' => 2, 'price' => 400, 'discount' => '50'])
);

Then you have a collection of objects !

然后你有一个对象的集合!

回答by Margus Pala

Function collect() takes array of elements. You can fix your code also by enclosing all the elements into [] and then converting Collection elements to objects instead of leaving them to be arrays

函数 collect() 接受元素数组。您也可以通过将所有元素包含在 [] 中,然后将 Collection 元素转换为对象而不是将它们保留为数组来修复代码

    $myCollection = collect([
        (object)(['product_id' => 1, 'price' => 200, 'discount' => '50']),
        (object)(['product_id' => 2, 'price' => 400, 'discount' => '50'])
            ]
    );
    foreach ($myCollection as $product) {
        echo $product->price;
        echo $product->discount;
    }

回答by edcs

There's a syntax error in your collection definition:

您的集合定义中存在语法错误:

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
]);

Also, since you're using a Collection, you no longer need to use a foreachloop:

此外,由于您使用的是 a Collection,您不再需要使用foreach循环:

$myCollection->each(function ($item) {
    echo $item['price'];
})