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

How to use every array first item

我正在使用 Laravel 5.7

开发项目

我有两个数组

我想查看这个数组table

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 看起来像这样

<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>

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

我做错了什么

你能试试这个吗.. 希望有用

@for ($i = $delegateGroup; $i < count($delegateGroup); $i++)
    <td> {{ $delegateGroup[i][0]  </td>
@endfor

在您发表评论后,我意识到我没有正确阅读您的问题 - 抱歉。

我认为一个简单的解决方案是用户 array_column https://www.php.net/manual/en/function.array-column.php

那么你可以这样说:

$tableheadings = array(['branch','report','product','cost'])[0];
$tableData = array( 
  [
    'branch' =>['branch.add','branch.delete'],
    'report' =>['report.create','report.list'],
    'product' =>['product.add','product.sell'],
    'cost' =>['cost.add','cost.list']
  ]
);

$table = '<thead>' . 
            implode('',array_map(function($th){return '<th>' . $th . '</th>';},$tableheadings)) 
         . '</thead>';


for($i = 0;$i < sizeof($tableData[0]); $i++) {
      $row = array_map(function($td){return '<td>' . $td . '</td>';},array_column($tableData[0],$i));
      if(sizeof($row) == 0) continue;
        $table .= '<tr>' .implode('',$row)  . '</tr>';
}

    echo '<table>' . $table . '</table>';

我已经在 phpfiddle 上测试过了,它给出了你想要的结果,如果你能以稍微更好的格式获取数据,你可以使它更简单(我不确定你从哪里得到它)

我做到了

感谢@jameson2012 的想法

之前找到最长的数组
$rowCount = 0;
array_walk($delegateType, function ($items) use (&$rowCount) {
            $count = count($items);
            if ($count > $rowCount)
                $rowCount = $count;
        });

的 $delegateType 回显索引之后

并且每个数组的第一个值回显

<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>