简体   繁体   English

在foreach循环中通过单个项目进行评估

[英]Issue ierating through individual items in a foreach loop

I have got an array collection whereby I am iterating through each of them individually and get each id so as to display in the view. 我有一个数组集合,借此我分别遍历每个数组并获取每个id以便显示在视图中。 When using a foreach loop I only get the 1st id but I want to get all of them depending on the number of items in the array. 当使用foreach循环时,我只会得到第一个ID,但我想根据数组中的项目数来获得所有ID。 For instance in the array before I should get 4 ids. 例如在数组之前,我应该得到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]
  ]
]

Iterating in the view 在视图中迭代

 @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

You can use the array_map function. 您可以使用array_map函数。

$ids = array_map(function ($item) {
    return $item['id'];
}, $array);

Which will return a new array after applying the callback on the old one. 在对旧数组应用回调之后,它将返回一个新数组。

If you call dd() , script will be dump first element content and stop execute. 如果调用dd() ,脚本将转储第一个元素的内容并停止执行。 Just remove dd() to display all elements. 只需删除dd()即可显示所有元素。

<a data-toggle="collapse" href="#collapse1"> {{ $asms['id'] }}</a>

I think nested foreach will do the trick. 我认为嵌套的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM