Looping through array containing arrays - php

I have completely changed the format of my array now, and I had what I hope is a simple misunderstanding on my part. So my array now looks like the following
array:9 [▼
0 => array:4 [▼
"leadData" => array:7 [▼
"LeadID" => "1232806"
"Client" => "Some Client"
"LeadName" => "Test"
"Owner" => "Someone"
"Value" => "2160.00"
]
"clientData" => array:2 [▼
"Prospect" => "No"
]
"quoteData" => array:8 [▼
"QuoteID" => "Q0020"
"ProjectName" => "Test"
"Amount" => "1234"
]
"customData" => array:2 [▼
0 => array:1 [▼
"Type" => "New"
]
1 => array:1 [▼
"Month" => "June 16"
]
]
]
2 => array:4 [
...
]
]
So it is essentially now 4 inner arrays. Now if I do the following, I can print out all the data for the leadData inner array
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData }}
</td>
endforeach
</tr>
endforeach
That works fine. However, I only want to display certain parts of this array. I would have presumed doing something like the following would work
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData['LeadID'] }}
</td>
<td>
{{ $leadData['LeadName'] }}
</td>
endforeach
</tr>
endforeach
However if I do this I get and Illegal String Offset error. Is this not how I would access this data?
p.s. Ignore the way I do the foreach loop etc, this is because I am using a template engine.
Thanks

You don't have to loop over the second array, you can use the keys to get the data.
foreach($leadArray as $array)
<tr>
<td>
{{ $array['leadData']['LeadID'] }}
</td>
<td>
{{ $array['leadData']['LeadName'] }}
</td>
</tr>
endforeach

Your main array will be like this, where you want to work, right?
<pre>
$aMainArray = array(
0 => array(
"leadData" => array(
"LeadID" => "1232806",
"Client" => "Some Client",
"LeadName" => "Test",
"Owner" => "Someone",
"Value" => "2160.00",
)
)
);
foreach ($aMainArray AS $aSubArray) {
print_r($aSubArray);
// You can echo your required values like below
echo $aSubArray['leadData']['LeadID'];
echo $aSubArray['leadData']['LeadName'];
// OR like this one
foreach ($aSubArray AS $value) {
echo $value['LeadID'];
echo $value['LeadName'];
}
}
</pre>

Related

Avoid duplicate values in foreach laravel session array

Hi i need to solve this case in session cart that have array in session
The size duplicated many times in blade like this
Size:
large
large
large
array:1 [▼ // app/Http/Controllers/Controller.php
1 => array:3 [▼
"id" => 1
"quantity" => 36
"color_items" => array:3 [▼
0 => array:2 [▼
"color" => "3"
"size" => "2"
]
1 => array:2 [▼
"color" => "2"
"size" => "2"
]
2 => array:2 [▼
"color" => "1"
"size" => "2"
]
]
]
]
<dl class="card-item-desc-1">
<dt>Size:</dt> #foreach($details['color_items'] as $de)
<dd>
{{ \App\Models\Size::find($de['size'][0])->name }}
</dd>
#endforeach
</dl>
You have a couple of options.
First strip the size out of the color_items array and use as its own array.
Second would be to use the power of collections
Change the foreach to
#foreach(collect($details['color_items'])->pluck('size')->unique() as $size)
<dd>
{{ \App\Models\Size::find($size)->name }}
</dd>
#endforeach
So what we are doing is adding the color_items to a collection, taking only the size field and then making sure its unique.
Hope that helps

undefined index 'value' in laravel

im using laravel 7 and trying to get datas from api use http.
when i use dd('$datas'),i got this
array:1 [▼"manajemen_sdm" => array:4 [▼ 0 => array:9 [▼ "id_karyawan" => "2" "nama" => "Koko Hendriko" "gender" => "Laki-Laki" "ttl" => "1998-04-07" "alamat" => "Kp.Palasari " "no_telp" => "821863141" "email" => "kokohendriko#gmail.com" "npwp" => "89678923652" "golongan" => "B" ] 1 => array:9 [▶] 2 => array:9 [▶] 3 => array:9 [▶] ] ]
my controller
public function index()
{
$datas = Http::get('https://projectsoadenis.000webhostapp.com/api')->json();
return view('sdm.index',compact('datas'));
}
view
#foreach ($datas as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach
If you look at the data you are getting back from the API, your data is actually nested inside objects that live under a manajemen_sdm property.
Your loop should work correctly if you update it to loop over the manajemen_sdm key:
#foreach ($datas['manajemen_sdm'] as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach

how to set multiple title as heading only once and depending values under it Laravel

I have a ecommerce website which shows multiple variants like size, color etc...I have create a products details array as below.
array:1 [▼
0 => array:12 [▼
"proId" => 2268
"name" => "PAMPERS BABY DRY PANTS LARGE -64 PANTS"
"oprice" => 1099.0
"ofprice" => 1044.05
"slug" => "pampers-pants-large-64s-9-14kg"
"variants" => array:5 [▼
0 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 567
"items_value" => "42 NOS"
"sku" => "1058898"
"ean" => "4902430645577"
"mrp" => 699.0
"mfp" => 664.05
"combo" => ""
]
]
]
1 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 568
"items_value" => "24 pants"
"sku" => "PM50"
"ean" => "4902430900485"
"mrp" => 399.0
"mfp" => 379.05
"combo" => ""
]
]
]
2 => array:2 [▶]
3 => array:2 [▶]
4 => array:2 [▼
"title" => "Weight"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 591
"items_value" => "100gm"
"sku" => "wet"
"ean" => "346"
"mrp" => 436.0
"mfp" => 33.0
"combo" => ""
]
]
]
]
]
]
The code for the above is,
$product = Products::select('products.*', 'brand.name', 'category.id as cat_id')->join('category', 'category.id', '=', 'products.category_id')->join('subcategory', 'subcategory.id', '=', 'products.subcategory_id')->join('brand', 'brand.id', '=', 'products.brand_id', 'left')->where(array('products.slug' => $slug, 'products.status' => 1))->get();
$datas = [];
foreach ($product as $pro) {
$item = [
"proId" => $pro->id,
"name" => $pro->title,
"oprice" => $pro->org_price,
"ofprice" => $pro->off_price,
"slug" => $pro->slug,
"variants" => []
];
$variantlist = Provariants::select('products_variants.id', 'variants.name', 'products_variants.variants_type_id', 'products_variants.sku', 'products_variants.ean', 'products_variants.price', 'products_variants.mfp', 'products_variants.combo')->join('variants_type', 'variants_type.id', '=', 'products_variants.variants_type_id')->join('variants', 'variants.id', '=', 'variants_type.variants_id')->where(array('products_variants.products_id' => $pro->id, 'products_variants.status' => 1))->get();
foreach ($variantlist as $list) {
$typelist = Variantstype::where(array('id' => $list->variants_type_id, 'status' => 1))->get();
$subitems = [];
foreach ($typelist as $type) {
$subitems[] = [
"items_id" => $list->id,
"items_value" => $type->name,
"sku" => $list->sku,
"ean" => $list->ean,
"mrp" => $list->price,
"mfp" => $list->mfp,
"combo" => $list->combo
];
}
$item['variants'][] = [
"title" => $list->name,
"items" => $subitems,
];
}
$datas[] = $item;
}
Now the problem is, the title is showing multiple times and one items displaying under it. To get an idea check below image.
https://i.stack.imgur.com/tLjc9.png
I want title to be shown once and all the items under each title should be place together. Please check the required output.
https://i.stack.imgur.com/RFddf.png
Current result code is,
#if (count($pro['variants']) > 0)
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
<h6>{{ $variants['title'] }}</h6>
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
#endif
How can I achieve the required output?
A similar solution to the one by #sta, but I think this might be a little more flexible.
#php
$title = '';
#endphp
#foreach ($pro['variants'] as $variant)
<div class="variants mt-4">
<div>
#if($title !== $variant['title'])
<h6>{{ $variant['title'] }}</h6>
#endif
#foreach($variant['items'] as $var_item)
<p>{{ $var_item['items_value'] }}</p>
#endforeach
</div>
</div>
#php
$title = $variant['title'];
#endphp
#endforeach
I have not tested this, but it should write out the title every time $variant['title'] changes.
This will work :
#php
$x = 1;
#endphp
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
#if($x == 1)
<h6>{{ $variants['title'] }}</h6>
#endif
#php
$x += $x;
#endphp
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
Above code tested here

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

Get the value of arrays in array in twig

I have this array dataPP_ids coming from a controller
array:3 [▼
0 => array:1 [▼
"plan_participate_id " => 47
]
1 => array:1 [▼
"plan_participate_id " => 47
]
2 => array:1 [▼
"plan_participate_id " => 48
]
]
I just want to show the "plan_participate_id" values in twig file.
I tried
{% for details in dataPP_ids %}
<tr>
<td class="pl-0 align-middle text-center" rowspan="2">{{ details.plan_participate_id }}</td>
<td>---</td>
</tr>
values of details.plan_participate_id are not showing in the frontend. How am I able to display it in twig?
You seem to have a trailing space here.
"plan_participate_id "
Instead of
"plan_participate_id"
You should either remove it or access it in twig like that.
{{ details['plan_participate_id '] }}

Categories