Reputation: 109
I'm retrieving the JSON array from my DB and my array is:
{"Position":["first","second","third","fourth"],"Color":["Red",Blue,Pink,Teal]}
I want to show them in table inside a foreach loop
@foreach ($orderProduct->column as $item)
<thead>
<tr>
<th>{{ $item['Position'] }}</th>
<th>{{ $item['Color'] }}</th>
</tr>
</thead>
@endforeach
I want the output to be like this
<thead>
<tr>
<th>first</th>
<th>Red</th>
</tr>
</thead>
<thead>
<tr>
<th>second</th>
<th>Blue</th>
</tr>
</thead>
<thead>
<tr>
<th>third</th>
<th>Pink</th>
</tr>
</thead>
But it doesn't work.
Any ideas?
Upvotes: 1
Views: 1285
Reputation: 398
Because you're using Laravel, I would suggest to make use of the Collection
's zip
method that "merges together the values of the given array with the values of the original collection at their corresponding index" (Docs)
You would use it like this:
$newArray = collect($array['Position'])->zip($array['Color']);
@foreach($newArray as [$position, $color])
{{ $position }}
{{ $color }}
@endforeach
Upvotes: 1
Reputation: 15786
@foreach (array_combine($orderProduct['Position'], $orderProduct['Color']) as $position => $color)
<thead>
<tr>
<th>{{ $position }}</th>
<th>{{ $color }}</th>
</tr>
</thead>
@endforeach
Upvotes: 1
Reputation: 13535
@for ($i = 0; $i < count($orderProduct->column['Position']); $i++)
<thead>
<tr>
<th>{{ $orderProduct->column['Position'][$i] }}</th>
<th>{{ $orderProduct->column['Color'][$i] }}</th>
</tr>
</thead>
@endfor
Upvotes: 2