0

This has really stumped me, not sure why I cant get it working.

I have an a variable called 'posts'. Which contains the following.

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

I want to use twig to display the values in the nested arrays within the data array using twig markup.

At the moment I have the follow

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

I have also tried the following with no success.

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

any guidance on where i am going wrong would be great. Thanks.

1
  • Hi, thanks for the suggestion. I tried both and they are not working. Commented Nov 6, 2016 at 5:13

2 Answers 2

1

Post looks like this:

//<?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' 
        ]
    ]
];

There's one more array in the way: post.data[i].message

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for all the help everyone. Once again it was a basic error by myself. I forgot to pass the variable through when rendering the page using slim.

The correct markup in the end was very basic

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

thanks again!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.