Mamulasa Mamulasa - 1 month ago 12
PHP Question

Laravel 5 Loop Through Array

I am passing a variable ($mailchimp) from my Controller to my View.

this is what I got with {{dd($mailchimp)}}

array:8 [▼
"id" => "xyz123"
"email_address" => "john.doe@discworld.com"
"unique_email_id" => "c9a36649c8"
"email_type" => "html"
"status" => "subscribed"
"merge_fields" => array:2 [▼
"FNAME" => "John"
"LNAME" => "Doe"
]
"stats" => array:2 [▼
"avg_open_rate" => 0
"avg_click_rate" => 0
]
"list_id" => "769808qeqw92"
]


how can I loop through this array ($mailchimp) ? With the code below I get an exception: "htmlentities() expects parameter 1 to be string, array given"

@foreach($mailchimp as $user)
@if(is_array($user))
@foreach($user as $key => $value)
{{$value}}
@endforeach
@endif
@endforeach

Answer

You don't need to iterate over $user. If $mailchimp is an array of users, do this:

  {{ $mailchimp['email_adress'] }}
  {{ $mailchimp['merge_fields']['FNAME'] }} {{ $mailchimp['merge_fields']['LNAME'] }}