tags:

views:

302

answers:

2
Array
(
    [0] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => Pop music
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => Rock music
                )

            [2] => SimpleXMLElement Object
                (
                    [0] => Music edition
                )

        )

    [1] => Array
        (
            [3] => SimpleXMLElement Object
                (
                    [0] => Pop music
                )

            [4] => SimpleXMLElement Object
                (
                    [0] => Rock music
                )

            [5] => SimpleXMLElement Object
                (
                    [0] => Opera music
                )

        )

    [2] => Array
        (
            [6] => SimpleXMLElement Object
                (
                    [0] => Advs music
                )

            [7] => SimpleXMLElement Object
                (
                    [0] => Rington
                )

            [8] => SimpleXMLElement Object
                (
                    [0] => Game 
                )
        )

)

I have an array like above but I don't know how to loop it with foreach or section in smarty. Is anyone can give me a hand for this? I spend a daytime for it so please give me some idea. Thank you anyway!!

A: 

You could do nested foreach statements

foreach($array as $childArray)
{
    foreach($childArray as $obj)
    {
        echo $obj->0;
    }
}

Although I'm not sure variables are properly named if they have a number at the first character, so you might need to access it using brackets

$obj->{0};
Chacha102
Thank chacha102 so much but I write it in smarty not PHP so your code could not run!
gacon
Then why did you tag your question as 'PHP'?
Chacha102
ah I forgot this I just think Its belong to smarty and PHP, maybe its made you think like that. I'm sorry man!
gacon
A: 
{foreach from=$menu key=k item=v name=menu}
    {if $smarty.foreach.menu.index == 0}
        VietMusic
    {/if}
    {if $smarty.foreach.menu.index == 1}
        ForeignMusic
    {/if}
    {if $smarty.foreach.menu.index == 2}
        SpecialMusic
    {/if}
        {foreach from=$v item=submenu}
             {$submenu}
        {/foreach}
    {/foreach}

I'm write out my array like that but It's not optimize, Is anyone give another idea for this?

Finally I can do it like that,

{foreach from=$menu key=k item=v name=menu}
    {foreach from=$v item=submenu name=sub}
        {if $smarty.foreach.sub.first}
            {$submenu}
        {else}
            {$submenu}
        {/if}
    {/foreach}
{/foreach}
gacon