来自阵列的Laravel Foreach

时间:2015-02-22 20:39:27

标签: php arrays laravel foreach eloquent

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

            )

    )
)

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

我尝试过循环,例如;

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

我尝试过其他变种,但无济于事。什么是正确的语法?此外,有人可以解释我如何解释这一点,以便将来更好地了解foreach与数组?我通常只是猜测,直到我得到正确的结果 - 但是为了改变我真的想知道如果把它放在一起,如果这是有道理的。

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

2 个答案:

答案 0 :(得分:7)

你正在循环的不是一个数组。它是Laravel系列。但它的行为就像一个数组,所以它并不重要。循环本身实际上看起来正确。但是,您不必仅输出$country,而是必须实际访问名为$country的{​​{1}}上的属性:

country

通常,@foreach($locations['country'] as $location) {{ $location->country }} @endforeach 循环遍历数组或集合中的每个项目,并将该项目放入foreach之后定义的变量中。也许this explanation也有帮助。


稍微多一点:Laravel有一个很好的as函数,可以从集合中每个模型的属性构建一个数组。

lists()

会导致类似:

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

然后您可以使用['England', 'Wales', 'Scotland'] 之类的函数生成以逗号分隔的列表:

implode()

答案 1 :(得分:-1)

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