为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
长风秋雁 2021-04-07 13:15:15
我正在使用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>第一个数组中的第二个数组用于正确结果,但其他数组结果错误我究竟做错了什么
查看完整描述

3 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

你可以试试这个吗..希望它能工作


@for ($i = $delegateGroup; $i < count($delegateGroup); $i++)

    <td> {{ $delegateGroup[i][0]  </td>

@endfor


查看完整回答
反对 回复 2021-04-23
?
jeck猫

TA贡献1909条经验 获得超7个赞

找到之前最长的数组


$rowCount = 0;

array_walk($delegateType, function ($items) use (&$rowCount) {

            $count = count($items);

            if ($count > $rowCount)

                $rowCount = $count;

        });

在$ delegateType之后的回显索引


和每个数组的第一个值echo


<table>

    <thead>

    <tr>

        @foreach(array_keys($delegateType) as $group)

            <th>

                @if(isset($group))

                    $group

                @endif

            </th>

        @endforeach

    </tr>

    </thead>

    <tbody>

        @for ($i = 0; $i < $rowCount; $i++)

            <tr>

            @foreach($delegateType as $group => $values)

                <td>

                    @if(isset($values[$i]))

                          {{$values[$i]}}

                    @endif

                </td>

            @endforeach

            </tr>

        @endfor    

    </tbody>


查看完整回答
反对 回复 2021-04-23
  • 3 回答
  • 0 关注
  • 165 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信