我有一个数组集合,借此我分别遍历每个数组并获取每个id,以便在视图中显示。当使用foreach循环时,我只会得到第一个ID,但我想根据数组中的项目数来获得所有ID。例如,在数组中我应该得到4个ID。
array:4 [▼
0 => array:3 [▼
"id" => "157"
"unit_sales_managers" => array:7 [▶]
"policies" => array:3007 [▶]
]
1 => array:3 [▼
"id" => "73401"
"unit_sales_managers" => array:8 [ …8]
"policies" => array:2226 [ …2226]
]
2 => array:3 [▼
"id" => "0"
"unit_sales_managers" => array:1 [ …1]
"policies" => array:162 [ …162]
]
3 => array:3 [▼
"id" => "76300"
"unit_sales_managers" => array:1 [ …1]
"policies" => array:1 [ …1]
]
]
在视图中迭代
@foreach ($asm as $asms)
<div class="panel-group" id="hierachy">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1"> {{ dd($asms['id']) }}</a>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
@endforeach
答案 0 :(得分:1)
您可以使用array_map功能。
<div class="nest" onclick="nested.selection('VALUE1',event); return false;">
... something inside the div ...VALUE1
</div>
<div class="nest" onclick="nested.selection('VALUE2',event); return false;">
... something inside the div ...VALUE2
</div>
在对旧数组应用回调之后,它将返回一个新数组。
答案 1 :(得分:0)
如果调用dd()
,则脚本将转储第一个元素的内容并停止执行。只需删除dd()
即可显示所有元素。
<a data-toggle="collapse" href="#collapse1"> {{ $asms['id'] }}</a>
答案 2 :(得分:-1)
我认为嵌套的foreach可以解决问题。
@foreach ($asm as $asms)
@foreach ($asms as $a)
<div class="panel-group" id="hierachy">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1"> {{ dd($a['id']) }}</a>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
@endforeach
@endforeach