Samuel Henry
Samuel Henry

Reputation: 153

How could i get value from array of array in laravel

Helper, I can fetch value of fname, lname and so on.., But I'm struggling to fetch value of address1, city.., where my array is,

Array
(
    [id] => 172
    [fname] => Someone
    [lname] => Sombody
    [gender] => Male
    [phno] => 0123456789
    [addresses] => Array
        (
            [0] => Array
                (
                    [id] => 71
                    [user_id] => 172
                    [address1] => somewhere
                    [city] => some city
                    [state] => some state
                    [country] => India
                )
            [1] => Array
                (
                    [id] => 72
                    [user_id] => 172
                    [address1] => someplace
                    [city] => specified city
                    [state] => specified state
                    [country] => India
                )
        )
)

Upvotes: 2

Views: 79

Answers (3)

Pranay Majmundar
Pranay Majmundar

Reputation: 867

You can access by $baseArray['addresses'][0]['address1'] and so on.

But what you probably want is something like:

@foreach ($array as $baseArray['addresses'])
    {{ $address['city'] }}
    {{ $address['address1'] }}
@endforeach

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15971

If you really want to this in Laravel way, you can use a collection.

Here is an example

 $collection = collect(Your array);

$filtered = $collection->only(['addresses']);
$filtered->all();
//it will return the your desired collection

Hope this helps

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

You need to iterate over subarray, for example:

@foreach ($array['addresses'] as $address)
    {{ $address['city'] }}
    {{ $address['address1'] }}
@endforeach

Upvotes: 0

Related Questions