【问题标题】:Laravel Foreach From ArrayLaravel Foreach 来自数组
【发布时间】:2015-04-24 03:19:56
【问题描述】:

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

【问题讨论】:

    标签: php arrays laravel foreach eloquent


    【解决方案1】:

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

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

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


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

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

    会产生类似的结果:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-10
      • 2021-01-13
      • 2012-10-25
      • 2016-08-24
      • 1970-01-01
      • 2021-04-01
      • 2017-05-30
      • 2015-01-19
      相关资源
      最近更新 更多