Laravel php foreach Loop get value by key - php

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.

Related

How to use nested loop for different arrays in laravel blade template?

I am trying to display the distance for each of my drivers on my database in my view.
When I do die or dump for $pickupDistanceAndTime2 it returns the arrays below, so I need each of the drivers to have its own distance.
0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
return view('driverspane', [
'drivers' => $driver,
'rideDistancenTime' => $pickupDistanceAndTime2,
]);
In my view, I am looping but not getting the way I want it to appear.
#foreach ($drivers as $item)
#foreach ($rideDistancenTime as $data)
{{ $data[0] }}
#endforeach
#endforeach
But the above code keeps repeating distance and time for each of the driver, but I want each of the driver to have its own distance and time only, please see the result I am getting below attached.
The array for $driver returns
#items: array:3 [▼
0 => App\Driver {#317 ▶}
1 => App\Driver {#300 ▶}
2 => App\Driver {#281 ▶}
]
From what you did above, you are just repeating the same values for the first index of the array. You should not do a nested foreach statement.
Rather you could do it this way(using a for statement, provided that the arrays all have same number of keys:
#for ($i=0; $i<count($drivers); $i++)
{{$rideDistancenTime[$i][1]}}
#endfor
Now it should loop properly.
You can use below code to achieve your goal:
$rideDistancenTime;
#foreach ($drivers as $key => $item)
{{ $rideDistancenTime[$key] }}
#endforeach```

How to parse the value in blade laravel?

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

Laravel Loop Through Multidimesional Array

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

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

Using foreach on array in Laravel view

Why is my foreach loop fails with error "Trying to get property of non-object":
#foreach ($memberships as $membership)
{{ $membership->id }}
#endforeach
but this works just fine:
#foreach ($memberships as $membership)
<?php print_r($membership['id']); ?>
#endforeach
if I dd($memberships); I get
array:2 [▼
0 => array:1 [▼
"id" => 8
]
1 => array:1 [▼
"id" => 9
]
]
As was pointed out, I was trying to access array as an object...
#foreach ($memberships as $membership)
{{ $membership['id'] }}
#endforeach

Categories