• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Use stdClass in Templates

#1
Hi,

I have a stdClass variable which created in PHP, I use $template->create to pass it to my template.

this is my template code :
PHP:
<xen:foreach loop="$data" value="$i">
    {$i.name}<br>
</xen:foreach>
But because $i is an object I get this error:
Fatal error: Cannot use object of type stdClass as array in ...


How Can I use stdClass in my templates?


Regards
 

Chris D

XenForo developer
Staff member
#2
You can't unless it has array like behaviours such as implementing ArrayAccess or similar.

Is this related to your JSON data from the other thread? If so, json_decode supports outputting an associative array rather than an object:

PHP:
$array = json_decode($json, true);
 
#3
You can't unless it has array like behaviours such as implementing ArrayAccess or similar.

Is this related to your JSON data from the other thread? If so, json_decode supports outputting an associative array rather than an object:

PHP:
$array = json_decode($json, true);
Thanks again Chris ;)

Yes, This problem related to JSON question,
I found this solution earlier, but I think there is a way to use object.
anyway thank you for your help.