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
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
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'] }}
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
Related
Parse the laravel array data Am tried not show any data?
Please suggest to any other source?
here is the my dd Values in my laravel controller
array:2 [▼
"count" => 5
"date" => array:1 [▼
0 => array:5 [▼
0 => "2019-11-04 15:41:53"
1 => "2019-11-05 14:28:10"
2 => "2019-11-12 13:47:31"
3 => "2019-11-14 12:39:12"
4 => "2019-11-17 10:54:39"
]
]
]
am unable to parse the value on blade my tried
{{ $nov['date']['0']['0'] }} ---- > here i get the exact value "2019-11-04 15:41:53" i need parse in dynamic data
#foreach ($nov as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach #endforeach
Thanks
If you want to loop just the index 0 of date
#foreach ($nov['date']['0'] as $key => $value)
{{ $value}}
#endforeach
If you want to loop all the date
#foreach ($nov['date']['0'] as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach
#endforeach
All the dates are in the key of $nov['date'][0]. So you've to make a loop by using this key instead of $now.
Change in foreach loop and use $nov['date'][0] instead of direct $nav
#foreach ($nov['date'][0] as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach
if you print_r($now['date']) you'll get
0 => array:5 [▼
0 => "2019-11-04 15:41:53"
1 => "2019-11-05 14:28:10"
2 => "2019-11-12 13:47:31"
3 => "2019-11-14 12:39:12"
4 => "2019-11-17 10:54:39"
]
and then if you access 0 index like $now['date'][0] then you will get the following data which you need to make a loop.
array:5 [▼
0 => "2019-11-04 15:41:53"
1 => "2019-11-05 14:28:10"
2 => "2019-11-12 13:47:31"
3 => "2019-11-14 12:39:12"
4 => "2019-11-17 10:54:39"
]
Hope you understand.
Use this one.
#foreach ($nov['date'] as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach #endforeach
Here's my sample code.
$users = 3 users //suppose
$leaders = new Collection();
foreach($users as $user)
{
$name = $user->name;
$username = $user->username;
$leaders->put('name', $name);
$leaders->put('username', $username);
}
dd($leaders);
This gives me the result for only one user (The 3rd user)
Collection {#274 ▼
#items: array:2 [▼
"name" => "user3"
"username" => "username3"
]
}
Because put() is replacing the values with the same keys.
I tried with this too:
$leaders->push($name);
$leaders->push($username);
But I am getting:
Collection {#274 ▼
#items: array:6 [▼
0 => "user1"
1 => "username1"
2 => "user2"
3 => "username2"
4 => "user3"
5 => "username3"
]
}
How to create different item arrays for different users?
Update #1: Got the answer.
Now trying to display the values on the view like this:
#foreach($leaders as $leader)
{{ $leader->username }}
#endforeach
Error: Trying to get property of non-object.
Is this not supposed to work this way for custom collections?
Update #2: Nevermind. Found it.
It is supposed to work this way: {{ $leader['username'] }}
Just try push method
foreach($users as $user)
{
$leaders->push([
'name' => $user->name,
'username' => $user->username,
])
}
So i've problem to print the result in blade that the data are from controller, so :
UseController.php :
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => $expertCategory ];
return view('cms.user.edit', $data);
Category.php (models) getCategoryByID($user_id) return result array if i dd(expertCategory); in controller, which the result is :
array:9 [▼
"id" => 1
"name" => "Beauty"
"sequence" => 1
"background_color" => "ffffff"
"status" => "Active"
"created_at" => "2017-06-19 09:41:38"
"updated_at" => "2017-06-19 09:41:38"
"icon_filename" => "beauty-icon"
"iconURL" => array:3 [▼
"small" => "http://localhost:8000/images/category_icons/small/beauty-icon"
"medium" => "http://localhost:8000/images/category_icons/medium/beauty-icon"
]
]
But when i want to print using foreach the result in blade.php with code :
#foreach($expertCategory as $expertCat)
{{ $expertCat->id }}
#endforeach
will return error "Trying to get property of non-object "
if i use code like this :
#foreach($expertCategory as $expertCat)
{{ $expertCat['id'] }}
#endforeach
it will return : "Illegal string offset 'id'"
anybody can help solve this problem :s ? many thanks !
As $expertCategory is a one dimensional array you are facing this issue
Just Replace this
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => $expertCategory ];
return view('cms.user.edit', $data);
With
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => [$expertCategory] ];
return view('cms.user.edit', $data);
Then use
#foreach($expertCategory as $expertCat)
{{ $expertCat['id'] }}
#endforeach
In your blade it will work for you.
I am trying to loop through a multidimensional array in my view.
the array (I am passing $mailchimp from my controller to my view) is:
array:19 [▼
"id" => "f3200e9cc5a900bb7c075103b871232f0"
"email_address" => "john.doe#discworld.com"
"unique_email_id" => "xalasd"
"email_type" => "html"
"status" => "subscribed"
"merge_fields" => array:2 [▼
"FNAME" => "John"
"LNAME" => "Doe"
]
"stats" => array:2 [▶]
"ip_signup" => ""
"timestamp_signup" => ""
"ip_opt" => "93.212.91.32"
"timestamp_opt" => "2016-10-27T13:53:02+00:00"
"member_rating" => 2
"last_changed" => "2016-10-27T13:53:02+00:00"
"language" => ""
"vip" => false
"email_client" => ""
"location" => array:6 [▶]
"list_id" => "76980934492"
"_links" => array:8 [▶]
]
With this Code in my view:
#foreach($mailchimp as $user)
#foreach($user as $key => $value)
<ul>
<li>{{$value}}</li>
</ul>
#endforeach
#endforeach
An exception is thrown: Invalid argument supplied for foreach()
Can somebody tell me how to fix this ?
you are expecting for the value of each of the first array to also be an array. That is not the case, only some values from the first array is an array, so you must put a condition. You can use the is_array helper to see if the value from the first array is an actual array, if so, loop thru each one of those.
foreach($a as $b){
if(is_array($b)){
foreach($b as $c){
echo($c);
}
}
}
As mentioned by Carlos the main issue you're encountering is because you're trying to echo an array find his answer here.
Regarding your second issue Thanks Carlos. It tried you solution with this result: htmlentities() expects parameter 1 to be string, array given do you have any other code on that page, perhaps {{ Form::text('something', $array) }}
I think im getting this simple thing confused. I just want to get the value of my key 'weeks' and 'days'. I have tried the following:
#foreach($years as $key3 => $year)
<h1>{{$key3}}</h1>
#foreach($year as $key2 => $months)
<p>{{$key2}}</p>
#foreach($months as $key1 => $days)
<p>{{$days['weeks']}}</p>
<p>{{$days->weeks}}</p> //try two//
#endforeach
#endforeach
#endforeach
which responds with this error:
Illegal string offset 'weeks'
this is an example of the array im trying to loop:
array:4 [▼
2016 => array:12 [▼
"01" => array:2 [▼
"weeks" => 5
"days" => "31"
]
can someone help me understand what I am doing wrong?
You don't need the last foreach,
#foreach($years as $key => $year)
<h1>{{$key}}</h1>
#foreach($year as $key => $months)
<p>{{$key}}</p>
{{ $months['weeks'] }}
{{ $months['days'] }}
#endforeach
#endforeach
Days isn't an array. But month is containing the keys: weeks and days.
If you want object notation (->) just cast it to an object by typing (object) before the array.