在刀片中循环数组数据

时间:2016-05-11 12:09:50

标签: laravel-5 laravel-5.1 laravel-blade

我进行了一些API调用并填充了一个数组。我已经确定了数组元素的三种不同方式

array:9 [▼
  0 => array:2 [▼
    "jobData" => array:5 [▶]
    "clientData" => array:3 [▶]
  ]
  5 => array:3 [▼
    "jobData" => array:5 [▶]
    "clientData" => array:3 [▶]
    "customData" => array:1 [▼
      "Type of work" => array:1 [▼
        "Type of work" => "Something"
      ]
    ]
  ]
  8 => array:3 [▼
    "jobData" => array:5 [▶]
    "clientData" => array:3 [▶]
    "customData" => array:2 [▼
      "Month" => array:1 [▼
        "Month" => "Aug 16"
      ]
      "Type of work" => array:1 [▼
        "Type of work" => "Something"
      ]
    ]
  ]
]

因此,元素0只有jobData和clientData。元素5有两个加上工作类型的cusomtData。元素8包含customData 适用于工作类型和月份。自定义数据将完全为空,包含一个或包含两者。

我试图在表格中显示这些数据。除了自定义数据之外,大部分内容都很简单。目前我有类似的东西

<table class="table table-bordered table-hover additionalMargin alignment reportTable">
    <tr class="col-md-12 noPadding">
        <thead>
        <tr>
            <th>Job ID</th>
            <th>Job Name</th>
            <th>Category</th>
            <th>Client Name</th>
            <th>Type of work</th>
            <th>Month</th>
        </tr>
        </thead>
    </tr>
    <tbody>
        @if(is_array($jobArray))
            @foreach($jobArray as $array)
                <tr>
                    <td>{{ $array['jobData']['JobID'] or '' }}</td>
                    <td>{{ $array['jobData']['JobName'] or '' }}</td>
                    <td>{{ $array['jobData']['Category'] or '' }}</td>
                    <td>{{ $array['clientData']['ClientName'] or '' }}</td>
                    <td>{{ $array['customData']['Type of work'] or '' }}</td>
                    <td>{{ $array['customData']['Month'] or '' }}</td>
                </tr>
            @endforeach
        @endif
    </tbody>
</table>

现在因为我的自定义数据中的元素也是数组,这意味着我需要为它们做另一个循环。所以我可能会这样做

@if(!empty($array['customData']))
    @foreach($array['customData'] as $data)
        <td>{{ $data['Type of work'] or '' }}</td>
        <td>{{ $data['Month'] or '' }}</td>
    @endforeach
@endif

上面的问题是它似乎创建了自己的tds并且事情没有正确匹配。我基本上看到这样的事情 enter image description here 我怎样才能使事情适当地协调一致?

由于

2 个答案:

答案 0 :(得分:1)

您没有为$array['customData']填写<tr>的下一行做准备。这就是为什么你在第一列中有所有下一个结果。这是解决方案:

@if(is_array($jobArray))
        @foreach($jobArray as $array)
            <tr>
                <td>{{ $array['jobData']['JobID'] or '' }}</td>
                <td>{{ $array['jobData']['JobName'] or '' }}</td>
                <td>{{ $array['jobData']['Category'] or '' }}</td>
                @if(!empty($array['customData']))
                    @foreach($array['customData'] as $i => $data)
                        @if($i > 0)
                        <tr>
                            <td colspan="3"></td>
                        @endif
                            <td>{{ $array['clientData']['ClientName'] or '' }}</td>
                            <td>{{ $data['Type of work'] or '' }}</td>
                            <td>{{ $data['Month'] or '' }}</td>
                        </tr>
                    @endforeach
                @else
                        <td colspan="3"></td>
                    </tr>
                @endif
        @endforeach
    @endif

答案 1 :(得分:1)

这是解决您问题的可能方法。你在这里做的是检查“customData”对象中是否有任何数据,因为其他四个对象对于所有三种对象类型都是常量。

<table class="table table-bordered table-hover additionalMargin alignment reportTable">
<tr class="col-md-12 noPadding">
    <thead>
        <tr>
            <th>Job ID</th>
            <th>Job Name</th>
            <th>Category</th>
            <th>Client Name</th>
            <th>Type of work</th>
            <th>Month</th>
        </tr>
    </thead>
</tr>
<tbody>
    @if(is_array($jobArray))
        @foreach($jobArray as $array)
            @if(!empty($array['customData']))
                @foreach($array['customData'] as $data)
                    <td>{{ isset($array['jobData']['JobID']) ? $array['jobData']['JobID'] : '' }}</td>
                    <td>{{ isset($array['jobData']['JobName']) ? $array['jobData']['JobName'] : '' }}</td>
                    <td>{{ isset($array['jobData']['Category']) ? $array['jobData']['Category'] : '' }}</td>
                    <td>{{ isset($array['clientData']['ClientName']) ? $array['clientData']['ClientName'] : '' }}</td>
                    <td>{{ isset($data['Type of work']) ? $data['Type of work'] : '' }}</td>
                    <td>{{ isset($data['Month']) ? $data['Month'] : '' }}</td>
                @endforeach
            @else 
                <td>{{ isset($array['jobData']['JobID']) ? $array['jobData']['JobID'] : '' }}</td>
                <td>{{ isset($array['jobData']['JobName']) ? $array['jobData']['JobName'] : '' }}</td>
                <td>{{ isset($array['jobData']['Category']) ? $array['jobData']['Category'] : '' }}</td>
                <td>{{ isset($array['clientData']['ClientName']) ? $array['clientData']['ClientName'] : '' }}</td>
            @endif
        @endforeach
    @endif
</tbody>

如果$array['customData'] 不是为空,则会为每个唯一类型的工作和/或月份填充ID,名称,类别和客户名称的相同四个值。如果$array['customData'] 为空,则只会打印ID,名称,类别和客户名称,然后转到下一个作业ID。