2016-11-05 60 views
0

這真的讓我難住,不知道爲什麼我不能讓它工作。使用Twig嵌套數組問題

我有一個名爲'posts'的變量。其中包含以下內容。

array(2) { ["data"]=> array(5) { 
    [0]=> array(3) { 
     ["full_picture"]=> string(98) "https://www.website.com/picture.jpg" 
     ["message"]=> string(613) "long message here" 
     ["id"]=> string(32) "000000000" } 
    [1]=> array(3) { 
     ["full_picture"]=> string(98) "https://www.website.com/picture.jpg" 
     ["message"]=> string(613) "long message here" 
     ["id"]=> string(32) "000000000" } 
    [2]=> array(3) { 
     ["full_picture"]=> string(98) "https://www.website.com/picture.jpg" 
     ["message"]=> string(613) "long message here" 
     ["id"]=> string(32) "000000000" } 
    [3]=> array(3) { 
     ["full_picture"]=> string(98) "https://www.website.com/picture.jpg" 
     ["message"]=> string(613) "long message here" 
     ["id"]=> string(32) "000000000" } 
    [4]=> array(3) { 
     ["full_picture"]=> string(98) "https://www.website.com/picture.jpg" 
     ["message"]=> string(613) "long message here" 
     ["id"]=> string(32) "000000000" } 
} 

["paging"]=> array(2) { 
    ["previous"]=> string(324) "website.com/link" 
    ["next"]=> string(306) "website.com/link" } 
} 

我想使用樹枝來顯示使用樹枝標記數據數組中的嵌套數組中的值。

此刻,我有如下

{% for post in posts %} 
{{ post.data.message }} 
{{ endfor }} 

我自己也嘗試沒有成功以下。

{% for key, post in posts %} 
{{ post.data.message }} 
{{ endfor }} 

任何指導我錯誤的地方會很好。謝謝。

+0

嗨,感謝您的建議。我試了兩次,他們都沒有工作。 –

回答

0

感謝所有幫助大家。再次,這是我自己的一個基本錯誤。使用slim渲染頁面時,我忘了傳遞變量。

到底是正確的標記是非常基本的

再次
{% for post in posts %} 
{{ post.full_picture }} 
{{ post.message }} 
{% endfor %} 

的感謝!

1

Post看起來是這樣的:

//<?php 

$post = [ 
    'data' => [ 
     // 
     0 => [ 
      'full_picture' => 'https://www.website.com/picture.jpg', 
      'message'  => 'long message here', 
      'id'   => '000000000' 
     ], 
     // 1..3 
     4 => [ 
      'full_picture' => 'https://www.website.com/picture.jpg', 
      'message'  => 'long message here', 
      'id' 
     ] 
    ] 
]; 

有一個在路上多了一個數組:post.data[i].message