我如何从laravel中的数组数组中获取值

时间:2018-03-01 12:34:11

标签: mysql arrays laravel

帮助器, 我可以获取fname,lname等的值..但是我正在努力获取address1,city ..的值,我的数组在哪里,

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

3 个答案:

答案 0 :(得分:1)

如果你真的想用Laravel方式,你可以使用collection

这是一个例子

 $collection = collect(Your array);

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

希望这有帮助

答案 1 :(得分:0)

您可以$baseArray['addresses'][0]['address1']等方式访问。

但你可能想要的是:

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

答案 2 :(得分:0)

您需要迭代子数组,例如:

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