2017-03-05 106 views
0

我對PHP Blade/Laravel很新穎,我試圖在HTML中顯示一個變量內的數組。PHP Laravel顯示陣列爲HTML

所以我就命名爲$products和首先我這樣做的一個變量:

{{ print_r($products) }} 

這給了我:

Array 
(
[0] => Array 
    (
     [title] => Belt 
     [image] => img/products/belt.jpg 
     [price] => 79.00 
     [specialPrice] => 
    ) 

[1] => Array 
    (
     [title] => Hat 
     [image] => img/products/hat.jpg 
     [price] => 89.00 
     [specialPrice] => 69.00 
    ) 

[2] => Array 
    (
     [title] => Bag 
     [image] => img/products/bag.jpg 
     [price] => 99.00 
     [specialPrice] => 59.00 
    ) 

[3] => Array 
    (
     [title] => Scarf 
     [image] => img/products/scarf.jpg 
     [price] => 119.00 
     [specialPrice] => 
    ) 

) 
1 

所以,我想顯示此陣爲HTML。我試過這個:

@foreach($products as $key => $item) 
     <p>{{@item->title}}</p> 
@endforeach  

但沒有奏效。我究竟做錯了什麼?

+0

' - >'爲對象'[]'或'{}'是用於陣列。例如'$ item ['title']'我認爲會這樣做。我也不熟悉laravel,但它可以直接在PHP中使用HTML嗎? – chris85

+0

Chris85是對的,只需使用方括號來顯示屬性即可。 –

+0

當我寫'

{{@item ['title']}}

'我得到'

'? – Steve

回答

0

嘗試這種情況:

@foreach($products as $product) 
     <p>{{$product->title}}</p> 
@endforeach 

或該

@foreach($products as $product) 
      <p>{{$product[title]}}</p> 
    @endforeach