Php 来自数组的Laravel Foreach

Php 来自数组的Laravel Foreach,php,arrays,laravel,foreach,eloquent,Php,Arrays,Laravel,Foreach,Eloquent,这是我的对象的打印\r Array ( [country] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Models\Location Object ( [connectio

这是我的对象的
打印\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
                    )

            )

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

我试过这样的循环:

@foreach ($locations['country'] as $country)
 {{ $country }}
@endforeach
我试过其他的方法,但都没有用。正确的语法是什么?另外,有人能解释一下我如何解释这一点,以便我将来更好地理解foreach和数组吗?通常情况下,我只是猜测,直到得到正确的结果——但对于一个变化,我想知道如何把一个放在一起,如果这是有意义的


如果有帮助的话,我使用的是Laravel…

你循环的不是数组。这是拉威尔的收藏。然而,它的行为就像一个数组,所以它实际上并不重要。实际上,循环本身看起来是正确的。但是除了输出
$country
之外,您还必须实际访问
$country
上名为
country
的属性:

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

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


另外:Laravel有一个很好的
lists()
函数,它从集合中每个模型的属性中构建一个数组

$countries = $locations['country']->lists('country');
会导致如下结果:

['England', 'Wales', 'Scotland']
然后,您可以使用类似于
内爆()
的函数来生成逗号分隔的列表:

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

你循环的不是数组。这是拉威尔的收藏。然而,它的行为就像一个数组,所以它实际上并不重要。实际上,循环本身看起来是正确的。但是除了输出
$country
之外,您还必须实际访问
$country
上名为
country
的属性:

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

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


另外:Laravel有一个很好的
lists()
函数,它从集合中每个模型的属性中构建一个数组

$countries = $locations['country']->lists('country');
会导致如下结果:

['England', 'Wales', 'Scotland']
然后,您可以使用类似于
内爆()
的函数来生成逗号分隔的列表:

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

为了更好地理解数组,我建议下载一个名为coderunner的应用程序,它可以在osx下工作,我相信你可以找到windows或linux的替代方案,你可以做的是组成任意级别或数组,并使用foreach语句更好地理解它们的工作原理。如果您愿意,我也可以帮助您,请给我发送电子邮件:sarmenhb@gmail.com

为了更好地理解阵列,我建议下载一个名为coderunner的应用程序,它在osx下工作,我相信你可以找到windows或linux的替代方案,你可以做的是组成任何级别或数组,并使用foreach语句更好地理解它们的工作原理。如果您愿意,我也可以帮助您,请给我发送电子邮件:sarmenhb@gmail.com