开发者

Is it possible to iterate over a multi-dimensional array in smarty?

开发者 https://www.devze.com 2022-12-10 13:38 出处:网络
Can someone provide a simple demo?I can\'t 开发者_开发百科seem to get my head around the smarty syntax for iterating over an array that looks like the one below.I want to get at values at all levels.

Can someone provide a simple demo? I can't 开发者_开发百科seem to get my head around the smarty syntax for iterating over an array that looks like the one below. I want to get at values at all levels.

Array
(
    [0] => Array
        (
            [id] => 0
            [name] => 12312
            [sub24] => Array
                (
                    [0] => Array
                        (
                            [classid] => 157
                            [classname] => 24011010
                        )

                    [1] => Array
                        (
                            [classid] => 170
                            [classname] => 24011020
                        )

                    [2] => Array
                        (
                            [classid] => 183
                            [classname] => 24011030
                        )

                )

            [sub23] => Array
                (
                    [0] => Array
                        (
                            [classid] => 60
                            [classname] => 23011010
                        )

                    [1] => Array
                        (
                            [classid] => 73
                            [classname] => 23011030
                        )

                    [2] => Array
                        (
                            [classid] => 85
                            [classname] => 23012030
                        )


                )

            [sub22] => Array
                (
                    [0] => Array
                        (
                            [classid] => 1
                            [classname] => 22011010
                        )

                    [1] => Array
                        (
                            [classid] => 13
                            [classname] => 22012010
                        )

                    [2] => Array
                        (
                            [classid] => 24
                            [classname] => 22012020
                        )

                )

        )


Not tested, but from the top of my head, something like this.

Note: I'm doing it like this because you said you wanted Smarty Syntax. Normally I'd stuff this into a helper function, create the list/table/whatever there, and just call the helper function from within Smarty.

<html>
<head><title>Smarty Foreach Demo</title></head>
<body>
<p>This code assumes you have $smarty->assign()'ed $var to be your multi-dimensional array</p>

<ul>
{foreach from=$var key=key1 item=lvl1}
    <li>Current key: $key1,
    Current value:
    {if !is_array($lvl1)}
       {$lvl1}
    {else}
       Array:<ul>
       {foreach from=$lvl1 key=key2 item=lvl2}
           <li>Current key: $key1,
           Current value:
           {if !is_array($lvl1)}
               {$lvl1}
           {else}
               Even deeper array.. keep doing same stuff!
           {/if}
           </li>
       {/foreach}
       </ul>
   {/if}
 {/foreach}
</ul>
</body>


You can do it like how you iterate over a PHP array:

See here for example.

foreach ($array as $i => $value) {
    PrintValue($i, $value);
}

function PrintValue($key, $val)
{
    echo $key;
   if(is_array($val))
   {
      foreach($val as $i=>$value)
      {
          PrintValue($i, $value);
      }
   }
   else
   {
      echo $val;
   }
}

Or, you can use $print_r($arr), this would print everything inside an array, including the array inside array element.


see smarty doc here (example 7.33)


{foreach from=$multi_array item=i}

    {if is_array($i) && count($i) > 0}

        {foreach from=$i item=j} <!-- if it is an array, lets create a loop from item=i, note the "$" in from=$i -->

            Class id: {$j.classid}<br />
            Class name: {$j.classname}<br />

        {/foreach}

    {else}

        {$i} <!-- will print id/name of your array -->

    {/if}

{/foreach}
0

精彩评论

暂无评论...
验证码 换一张
取 消