php 来自数组的 Laravel Foreach

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

Laravel Foreach From Array

phparrayslaravelforeacheloquent

提问by mikelovelyuk

Here is the print_rof my object;

这是print_r我的对象的;

Array
(
[country] => Illuminate\Database\Eloquent\Collection Object
    (
        [items:protected] => Array
            (
                [0] => App\Models\Location Object
                    (
                        [connection:protected] => 
                        [table:protected] => 
                        [primaryKey:protected] => id
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [country] => Scotland
                            )

                        [original:protected] => Array
                            (
                                [country] => Scotland
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [fillable:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                    )

                [1] => App\Models\Location Object
                    (
                        [connection:protected] => 
                        [table:protected] => 
                        [primaryKey:protected] => id
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [country] => England
                            )

                        [original:protected] => Array
                            (
                                [country] => England
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [fillable:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                    )

                [2] => App\Models\Location Object
                    (
                        [connection:protected] => 
                        [table:protected] => 
                        [primaryKey:protected] => id
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [country] => Wales
                            )

                        [original:protected] => Array
                            (
                                [country] => Wales
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [fillable:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                    )

                [3] => App\Models\Location Object
                    (
                        [connection:protected] => 
                        [table:protected] => 
                        [primaryKey:protected] => id
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [country] => 
                            )

                        [original:protected] => Array
                            (
                                [country] => 
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [fillable:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                    )

            )

    )
)

I want a foreach loop to print out the three countries in the countryarray - (England, Wales, Scotland).

我想要一个 foreach 循环来打印出country数组中的三个国家- (英格兰、威尔士、苏格兰)。

I have tried loops such as;

我尝试过循环,例如;

@foreach ($locations['country'] as $country)
 {{ $country }}
@endforeach

I have tried other variations of this but to no avail. What is the correct syntax? Also, can someone explain how I can interpret this so I can better understand foreach with arrays in future? I normally just end up guessing until I get the right result - but for a change I'd like to actually know how to put one together if that makes sense..

我尝试了其他变体,但无济于事。什么是正确的语法?另外,有人可以解释我如何解释这一点,以便我将来可以更好地理解带有数组的 foreach 吗?我通常只是猜测直到我得到正确的结果 - 但是对于改变,我想真正知道如何将一个组合在一起,如果这有意义的话..

I am using Laravel if it helps...

如果有帮助,我正在使用 Laravel...

回答by lukasgeiter

What you're looping over isn't an array. It's a Laravel Collection. However it behaves like an array so it doesn't really matter. The loop itself actually looks correct. But instead of just outputting $countryyou have to actually access the attribute on $countrycalled country:

你循环的不是数组。这是一个 Laravel 集合。然而,它的行为就像一个数组,所以它并不重要。循环本身实际上看起来是正确的。但不仅仅是输出,$country您还必须实际访问被$country调用的属性country

@foreach($locations['country'] as $location)
    {{ $location->country }}
@endforeach


In general a foreachloop goes over every item in an array or collection and put's that item into the variable you define after as. Maybe this explanationhelps too.

通常,foreach循环遍历数组或集合中的每个项目,并将该项目放入您在 之后定义的变量中as。也许这个解释也有帮助。



As a little extra: Laravel has a nice lists()function which builds an array out of an attribute from every model in a collection.

补充一点:Laravel 有一个很好的lists()函数,它根据集合中每个模型的属性构建一个数组。

$countries = $locations['country']->lists('country');

Would result in something like:

会导致类似:

['England', 'Wales', 'Scotland']

And you could then use functions like implode()to generate a comma separated list:

然后你可以使用函数implode()来生成一个逗号分隔的列表:

implode(', ', $countries); // returns 'England, Wales, Scotland'

回答by Sarmen B.

to better understand arrays i recomment downloading an app called coderunner which works under osx, im sure you can find alternatives for windows or linux and what you can do is make up any level or an array and play with the foreach statement to better understand how they work. i can also help you out if youd like, send me an email: [email protected]

为了更好地理解数组,我建议下载一个在 osx 下运行的名为 coderunner 的应用程序,我相信你可以找到 windows 或 linux 的替代品,你可以做的是组成任何级别或数组并使用 foreach 语句来更好地理解它们的方式工作。如果你愿意,我也可以帮助你,给我发电子邮件:[email protected]