如何使用每个数组的第一项

我正在使用Laravel 5.7开发项目


我有两个数组


我想查看表的这个数组


array(['branch','report','product','cost']);


array( 

[

       'branch' =>

          [

            'branch.add',

            'branch.delete, 

          ]

       'report' =>

          [

            'report.create',

            'report.list,  

          ]

       'product' =>

          [

            'product.add',

            'product.sell'

          ]

       'cost' =>

          [

            'cost.add',

            'cost.list

          ]

]

)

我希望桌子看起来像这样


<table>

   <thead>

      <th>Branch</th>

      <th>Report</th>

      <th>Product</th>

      <th>Cost</th>

   </thead>

   <tbody>

     <tr>

        <td>branch.add</td>

        <td>report.create</td>

        <td>product.add</td>

        <td>cost.add</td>

     </tr>

     <tr>

        <td>branch.delete</td>

        <td>report.list</td>

        <td>product.list</td>

        <td>cost.list</td>

     </tr>

   </tbody>

</table>

我做了很多尝试,但是无法编写正确的foreach循环。


第一次尝试


<table>

    <thead>

    <tr>

        @foreach($delegateGroup as $group)

            <th>{{$group}}</th>

        @endforeach

    </tr>

    </thead>

    <tbody>

        @foreach($delegateType as $delegate)

                @foreach($delegate as $v)

                    <tr>

                        <td>{{$v}}</td>

                    </tr>

                @endforeach

        @endforeach

    </tbody>

</table>

第一个数组中的第二个数组用于正确结果,但其他数组结果错误


我究竟做错了什么


长风秋雁
浏览 166回答 3
3回答

慕雪6442864

你可以试试这个吗..希望它能工作@for ($i = $delegateGroup; $i < count($delegateGroup); $i++)&nbsp; &nbsp; <td> {{ $delegateGroup[i][0]&nbsp; </td>@endfor

jeck猫

找到之前最长的数组$rowCount = 0;array_walk($delegateType, function ($items) use (&$rowCount) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count = count($items);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($count > $rowCount)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rowCount = $count;&nbsp; &nbsp; &nbsp; &nbsp; });在$ delegateType之后的回显索引和每个数组的第一个值echo<table>&nbsp; &nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; @foreach(array_keys($delegateType) as $group)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if(isset($group))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $group&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; @for ($i = 0; $i < $rowCount; $i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($delegateType as $group => $values)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if(isset($values[$i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{$values[$i]}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; @endfor&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </tbody>
打开App,查看更多内容
随时随地看视频慕课网APP