简体   繁体   中英

Access an object member inside an array of objects using Twig Engine

I have array of object if I using print_r looks like :

    Array
    (
        [0] => stdClass Object
            (
                [name] => User1
                [user_id] => 1
                [email] => user1@website.com
            )

        [1] => stdClass Object
            (
                [name] => User2
                [user_id] => 2
                [email] => user2@website.com
            )

        [2] => stdClass Object
            (
                [name] => User3
                [user_id] => 3
                [email] => user3@website.com
            )
     )

I want to loop and show them using Twig Engine then access member of object like name , email using Twig Engine too. How do that? I was tried but always error. I'm new about Twig.

Try this in Twig

{% for user in users %}
    <li><span>{{ user.name }}</span><span>{{ user.email}}</span></li>
{% endfor %}

Try this:

$object = your Std class Object;

foreach($object as $data)
{
    echo $data->name;
    echo $data->user_id;
    echo $data->email;
}

In twig you can use it like:

{% for user in users %}
    <li><span>{{ user.name }}</span><span>{{ user.email}}</span></li>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM