简体   繁体   中英

Laravel Foreach From Array

Here is the print_r of my object;

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 country array - (England, Wales, Scotland).

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..

I am using Laravel if it helps...

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 $country you have to actually access the attribute on $country called country :

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

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


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

$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(', ', $countries); // returns 'England, Wales, Scotland'

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: sarmenhb@gmail.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM