我打電話給一些API來填充陣列。最終的輸出是一樣的東西下面Laravel Blade - 顯示陣列內容
Array
(
[0] => Array
(
[leadData] => Array
(
[LeadID] => 1232806
[DateIdentified] => 21/04/2016
[Client] => Prospect 1
[LeadName] => Test
[Owner] => Some Owner
[Value] => 2160.00
[Status] => 70%
)
[clientData] => Array
(
[BusinessStructure] =>
[IsProspect] => No
)
[quoteData] => Array
(
[QuoteID] => Q0020
[ProjectName] => Test
[Amount] => 1800.00
[AmountTax] => 360.00
[AmountIncludingTax] => 2160.00
[EstimatedCost] => 450.00
[EstimatedCostTax] => 90.00
[EstimatedCostIncludingTax] => 540.00
)
[customData] => Array
(
[0] => Array
(
[Lead Type] => New
)
[1] => Array
(
[Month] => June
)
)
)
[1] => Array
(
[leadData] => Array
(
[LeadID] => 1230279
[DateIdentified] => 19/04/2016
[Client] => Bank1
[LeadName] => test 3
[Owner] => Some Owner
[Value] => 36000.00
[Status] => 50%
)
[clientData] => Array
(
[BusinessStructure] =>
[IsProspect] => No
)
[quoteData] => Array
(
[QuoteID] => Q0016
[ProjectName] => test 3
[Amount] => 30000.00
[AmountTax] => 6000.00
[AmountIncludingTax] => 36000.00
[EstimatedCost] => 0.00
[EstimatedCostTax] => 0.00
[EstimatedCostIncludingTax] => 0.00
)
)
)
有一點要注意的是,有時信息不是全部是存在的,只是因爲它是不可用的。所以你可以看到數組0有一些customData,而數組1沒有。其他元素甚至可能沒有quoteData部分等。
所以我的刀片模板現在有這個信息。我基本上想顯示所有的數據,但如果它不可用,只顯示一個空單元格。所以,我有以下標題
<table class="table table-bordered table-hover additionalMargin alignment">
<tr class="col-md-12 noPadding">
<thead>
<tr>
<th>Lead ID</th>
<th>Client Name</th>
<th>Is Prospect</th>
<th>Business Structure</th>
<th>Quote ID</th>
<th>Project Name</th>
<th>Amount</th>
<th>Amount Tax</th>
<th>Amount inc Tax</th>
<th>Cost</th>
<th>Cost Tax</th>
<th>Cost inc Tax</th>
<th>Type</th>
<th>Month</th>
</tr>
</thead>
</tr>
<tbody>
</tbody>
</table>
然而,TBODY內,我在做什麼,似乎很凌亂,我似乎在正確的地方,以獲得數據的唯一方法是,如果我如檢查一切
@if(is_array($leadArray))
@foreach($leadArray as $array)
<tr>
<td>
@if(!empty($array['leadData']))
{{ $array['leadData']['LeadID'] }}
@endif
</td>
<td>
@if(!empty($array['leadData']))
{{ $array['leadData']['Client'] }}
@endif
</td>
<td>
@if(!empty($array['clientData']))
{{ $array['clientData']['IsProspect'] }}
@endif
</td>
<td>
@if(!empty($array['clientData']))
{{ $array['clientData']['BusinessStructure'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['QuoteID'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['ProjectName'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['Amount'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['AmountTax'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['AmountIncludingTax'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['EstimatedCost'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['EstimatedCostTax'] }}
@endif
</td>
<td>
@if(!empty($array['quoteData']))
{{ $array['quoteData']['EstimatedCostIncludingTax'] }}
@endif
</td>
@if(!empty($array['customData']))
@foreach($array['customData'] as $data)
<td>
@if(!empty($data['Lead Type']))
{{ $data['Lead Type'] }}
@endif
</td>
<td>
@if(!empty($data['Month']))
{{ $data['Month'] }}
@endif
</td>
@endforeach
@else
<td></td>
<td></td>
@endif
</tr>
@endforeach
@endif
有沒有更好的方法來做到這一點?或者這是我唯一的選擇?
感謝
你可以做'{{@ $陣列[「leadData」] ['客戶端']}}'在數據丟失時抑制錯誤。或者以一種用'null'填充任何缺失數據的方式預處理數組。 – ceejayoz
@ceejayoz,有趣。它記錄在任何地方嗎?如果不是,它究竟是如何工作的? –
@AlexeyMezenin'@'是PHP的錯誤抑制運算符。有關詳細信息,請參閱http://php.net/manual/en/language.operators.errorcontrol.php。它應該很少使用,但你的看起來像一個潛在的適當的用例。 – ceejayoz