我正在将变量 $mailchimp
从我的 Controller 传递到我的 View 。
这是我用 {{dd($mailchimp)}}
array:8 [▼
"id" => "xyz123"
"email_address" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e2e7e0e6a6ece7edc8ece1fbebffe7fae4eca6ebe7e5" rel="noreferrer noopener nofollow">[email protected]</a>"
"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"
]
我如何循环遍历这个数组($mailchimp)?使用下面的代码我得到一个异常:“htmlentities() 期望参数 1 为字符串,给定数组”
@foreach($mailchimp as $user)
@if(is_array($user))
@foreach($user as $key => $value)
{{$value}}
@endforeach
@endif
@endforeach
更新: 在我的 Controller 中使用此代码
public function index()
{ //Fetch all subscribers from DB
$subscribers = Subscriber::where('user_id', Auth::user()->id)->orderBy('created_at','asc')->get();
foreach ($subscribers as $key => $subscriber) {
//Check if the local subscriber is also present in mailchimp
$mailchimp = Newsletter::getMember($subscriber->email);
}
return view('backend.newsletter.contacts.index')->withSubscribers($subscribers)
->withMailchimp($mailchimp);
}
我需要迭代 mailchimp 数组。由于有多个用户,alexey 的建议不再有效。
这个样式不起作用:
@foreach($mailchimp as $key => $user)
{{$user}}
@endforeach
最佳答案
您不需要迭代$user
。如果 $mailchimp
是用户数组,请执行以下操作:
{{ $mailchimp['email_adress'] }}
{{ $mailchimp['merge_fields']['FNAME'] }} {{ $mailchimp['merge_fields']['LNAME'] }}
关于php - Laravel 5 循环遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40376033/