How to loop multidimensional array in laravel blade - php

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>

Related

Laravel, How to view array in foreach loop

I want to view the loop of array in blade view, in my view i want print the array items.
I tried with this but it doesn't work
<div class="time-picker-container">
<div class="time-picker">
<ul>
#foreach($slots as $key => $slot)
<li>
<label class="time-picker-toggle-wrapper">
<input type="radio" value="" name="time-picker" />
<span class="time-name">{{ $slot->value }}</span>
</label>
</li>
#endforeach
</ul>
</div>
</div>
the array:
array:55 [▼
0 => array:2 [▼
"value" => "08:15"
"time-name" => "08:15 AM"
]
1 => array:2 [▼
"value" => "08:30"
"time-name" => "08:30 AM"
]
2 => array:2 [▼
"value" => "08:45"
"time-name" => "08:45 AM"
]
3 => array:2 [▼
"value" => "09:00"
"time-name" => "09:00 AM"
]
It's an array not object so you need to do {{ $slot['value'] }} not {{ $slot->value }}

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

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

Display array values in table format in Laravel 5.1

I have an array like so:
array:55 [▼
0 => array:13 [▼
"product_id" => "1"
"name" => "Glowing Antique Multi-Color Necklace And Hangings"
"product_code" => "DIV-COL-0001-0001"
"option_code" => ""
"net_price" => "693.00"
"sellerCommission" => "450.00"
"moderatorCommission" => "14.00"
"principalModeratorCommission" => "17.00"
"affiliateCommission" => "28.00"
"accountType" => "affiliate"
]
25 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
26 => array:13 [▼
"product_id" => "25"
"name" => "Side Hanging Purse (2 Nos.)"
"product_code" => "PRA-KEN-0002-0007"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
44 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "principal_moderator"
]
// And the list continues ...
]
The code that I have tried so far, works correctly to a certain extent.
<tr>
<th style="vertical-align: top;">Id</th>
<th style="vertical-align: top;">Details</th>
<th style="vertical-align: top;">Net Price</th>
<th style="vertical-align: top;">Invoicer<br /> Commission</th>
<th style="vertical-align: top;">Moderator Commission</th>
<th style="vertical-align: top;">Principal Moderator Commission</th>
<th style="vertical-align: top;">Affiliate Commission</th>
</tr>
#foreach($products as $product)
<tr>
<td>{{ $product['product_id'] }}</td>
<td>
<img src="{{ $product['image'] }}" alt="{{ $product['name'] }}" class="pull-left" style="margin-right: 15px">
{{ $product['name'] }}<br />
Product Code: {{ $product['product_code'] }}<br />
#if($product['option_code'] !== '')
Option Code: {{ $product['option_code'] }}
#endif
</td>
<td class="text-right">{{ $product['net_price'] }}</td>
#if($product['accountType'] === 'seller')
<td class="text-right">{{ number_format($product['sellerCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'moderator')
<td class="text-right">{{ number_format($product['moderatorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'principal_moderator')
<td class="text-right">{{ number_format($product['principalModeratorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'affiliate')
<td class="text-right">{{ number_format($product['affiliateCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
</tr>
#endforeach
The above code works, but it adds a new row to the table which is what I am not looking for. I do not want to add a new row, instead I would like to display the required data in that row only.
Meaning, for the product_id = 24, (except for the option code), there are 2 accountType attached viz., affiliate and principal_moderator. I would like to populate a single row with that data instead of 2 different rows.
How do I achieve it ??
I know this must be far easy, but I have failed to solve it, because I am still at the learning stage.
Any help is highly appreciated. Thanks.
P.S.: All the values are coming from the database, and the number of child arrays can be infinite.
You should group rows and make accountType property an array. Basically create new array, loop through products and add them into new array if they are not already added and if they are then add accountType to it. Something like this:
$grouped = array();
foreach($products as $product) {
if(!array_key_exists($product['product_id'], $grouped)) {
$grouped[$product['product_id']] = $product;
$grouped[$product['product_id']]['accountTypes'] = array($product['accountType']);
}else {
$grouped[$product['product_id']]['accountTypes'][] = $product['accountType'];
}
}
Then you use grouped data to display data and on accountType check you check if that type exists in accountTypes property.

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