2

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" => "[email protected]"
      "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

Update: With this Code in My 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);
}

I need to iterate the mailchimp array. As there are multiple users, alexey's suggestion doesn't work out anymore.

This stil doesn't work:

@foreach($mailchimp as $key => $user)
    {{$user}}
  @endforeach
2
  • is it possible that {{$value}} is an array? Commented Nov 2, 2016 at 9:11
  • is this array..what kind of array? Commented Nov 2, 2016 at 9:14

2 Answers 2

2

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'] }}
Sign up to request clarification or add additional context in comments.

10 Comments

with your suggestion i get this: " Illegal string offset 'email_address'"
@Mamulasa, please post {{ dd($mailchimp) }} result.
{{dd($mailchimp) is in my question above
@Mamulasa, if {{ dd($mailchimp) }} is an array that listed in your question above as you say, updated code will work for you.
Ah sorry, didn't remove the foreach
|
0

Since you are only interested in printing the values in your array, you can use array_flatten to get rid of the nested arrays, and then loop through the result:

@foreach(array_flatten($mailchimp) as $userData)    
    {{$userData}} 
@endforeach

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.