How to parse the value in blade laravel? - php

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

Related

How to loop multidimensional array in laravel blade

array:3 [▼
"name" => array:3 [▼
0 => "user2"
1 => "user3"
2 => "user4"
]
"email" => array:3 [▼
0 => "user2#gmail.com"
1 => "user3#gmail.com"
2 => "user4#gmail.com"
]
"phone_number" => array:3 [▼
0 => "90352065"
1 => "69856352"
2 => "903520658"
]
]
I'm getting above response from multiple text boxes now I need to loop in view, so How to loop above arrays in view??
how can I display above array values like in below format
User2 details
name : user2
email : user2#gmail.com
phone_number : 90352065
User3 details
name : user3
email : user3#gmail.com
phone_number : 69856352
User4 details
name : user4
email : user4#gmail.com
phone_number : 903520658
I presume below is your array structure
$original_array = [
"name" => [
"user2","user3","user4"
],
"email" => [
"user2#gmail.com","user3#gmail.com","user4#gmail.com"
],
"phone_number" => [
"90352065","69856352","903520658"
]
];
now to get desired output you can use below code
<dl>
#foreach($original_array['name'] as $key => $name)
<dt>{{ $name }} Details</dt>
<dd>
<ul>
<li>Name: {{ $name }}</li>
<li>Email: {{ $original_array['email'][$key] }}</li>
<li>Phone Number: {{ $original_array['phone_number'][$key] }}</li>
</ul>
</dd>
#endforeach
</dl>
kind of
#foreach($nameArray as $index => $nameArrayElement)
<tr>
<td>{{ $mainArray['name'][$index] }}</td>
<td>{{ $mainArray['email'][$index] }}</td>
<td>{{ $mainArray['phone_number'][$index] }}</td>
</tr>
#endforeach
Take in mind that the number of names are equal to the number of rows
In laravel I would use this approach to recurse a nested array in blade:
<ul>
#forelse($elements as $key => $item)
<li>
#if(is_array($item))
<strong>{{ $key }}: </strong>
#include('admin.payments.confirmation-data', ['elements' => $item, 'parentKey' => $key])
#else
<strong>{{ $key }}: </strong><span>{{ $item }}</span>
#endif
</li>
#empty
<li>
<strong>yay {{ $parentKey }}</strong>:<span>[]</span>
</li>
#endforelse
</ul>

Foreach with a multidimensional array - Laravel Blade templating

I have the following array result set, I'm trying to loop through each of the results and just echo them out onto the page. I'm using Laravel 5.2 and the blade templating engine
Collection {#240 ▼
#items: array:3 [▼
0 => array:2 [▼
"name" => "desktop"
"views" => "349"
]
1 => array:2 [▼
"name" => "mobile"
"views" => "151"
]
2 => array:2 [▼
"name" => "tablet"
"views" => "68"
]
]
}
This is what I have so far
#foreach($devices as $device)
$key = 0; $key++; $key < 2;
{{ $device[$key] }},
#endforeach
#foreach($devices as $device)
{{ $device->name }}
{{ $device->views}}
#endforeach
Will be enough.
You need to echo object properties:
#foreach($devices as $device)
{{ $device->name }} has {{ $device->views }}
#endforeach
If you like to use key then
#foreach($devices as $key => $val)
{{ $device[$key]->name }},
{{ $device[$key]->views }}
#endforeach

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

Laravel php foreach Loop get value by key

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.

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