Intelligent access to a specific element of the second array of measurements

I have a smarty template where the plugin loads an array for me. This array contains some elms, which themselves are arrays of stdClass objects, and other elements that are simply stdClass objects. For example, I could

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 like:

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

, div , , $memberOrGroup [0], " stdClass as array". , _r , , . print_r "is_array", {$ memberOrGroup [0] | print] r}, stdClass, , .

, foreach, ( $memberOrGroup [0] , $memberOrGroup [1]).

, , .

+3
2

:

$memberOrGroup[0]->id
+2

- smarty, , $memberOrGroup {}.

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

, .

+3

Source: https://habr.com/ru/post/1787348/


All Articles