3

I have a smarty template where a plugin loads an array for me. This array contains some elments which themselves are arrays of stdClass objects, and other elements which are simply stdClass objects. For instance, I might have

Array
(
[0] => Array
    (
        [0] => stdClass Object
            (
                [id] => 1
                [avatar_file] => joey_thumb.jpg
                [group] => 0
                [order_in_group] => 0
            )

        [1] => stdClass Object
            (
                [id] => 2
                [avatar_file] => christy_thumb.jpg
                [group] => 0
                [order_in_group] => 1
            )

    )
[1] => stdClass Object
    (
        [id] => 11
        [avatar_file] => angela_thumb.jpg
        [group] => 
        [order_in_group] => 
    )
)

In my template, I have something to the effect of:

{foreach from=$membersArray item=memberOrGroup}
    {if is_array($memberOrGroup)}
<div>{$memberOrGroup[0].id}</div>
    {else}
<div>{$memberOrGroup.id}</div>
    {/if}
{/foreach}

But as soon as it hits any normal replacement like in my div tags above that has an array reference like $memberOrGroup[0], I get the error "Cannot use object of type stdClass as array". I'm sort of at my whit's end, since I can print_r the original array and it shows the elements layed out like above. I can even print_r inside the "is_array" if block using {$memberOrGroup[0]|print)r} and it prints out an stdClass object populated with the right data and doesn't give me an error.

I've seen several solutions on the web that show using nested foreach loops, but in the real version of my loop each element of the second dimension array has to be placed in a certain spot (as in $memberOrGroup[0] has a special place, and so does $memberOrGroup[1]).

I've tried several variations including using sections instead, but I still come down to the same error.

2 Answers 2

3

I had to look very hard, because of the smarty syntax, but I see that you don't have your $memberOrGroup inside {}. So it should be

{foreach from=$membersArray item=memberOrGroup}
    {if is_array($memberOrGroup)}
<div>{$memberOrGroup[0].id}</div>
    {else}
<div>{$memberOrGroup.id}</div>
    {/if}
{/foreach}

I hope that will fix it.

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

2 Comments

Yeah, just noticed that I had left those off by accident while simplifying my use case. I actually had those in my template file. Thanks, though!
@Lythithwyn Ok, other thought: Is your stdClass an array maybe?
2

What about:

$memberOrGroup[0]->id

1 Comment

Doh! I new it must be something dumb! I don't use smarty often enough to know... (.) is for arrays, (->) is for objects. Gotcha!

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.