phptemplatingsmarty2

How do you access a nested array in smarty?


So, I am using Smarty2 and don't know Smarty. Trying to learn it as I go. I have this array that is structured like this (from Var dump)

array(59) {
    [0]=> array(4) {
        [0]=> string(10) "CCX 4PLY"
        [1]=> string(3) "SYP"
        [2]=> string(4) "4X8 "
        [3]=> array(6) {
            [0]=> string(0) ""
            [1]=> string(0) ""
            [2]=> string(0) ""
            [3]=> int(761)
            [4]=> string(0) ""
            [5]=> string(0) ""
        }
    }
    [1]=> array(4) {
        [0]=> string(5) " CCX"
        [1]=> string(3) "SYP"
        [2]=> string(4) "4X8 "
        [3]=> array(6) {
            [0]=> string(0) ""
            [1]=> string(0) ""
            [2]=> string(0) "" 
            [3]=> string(0) ""
            [4]=> int(823)
            [5]=> int(937)
        }
    }
    ...
}

The array is much longer, but that should be enough to give you an idea of what I have going. As you can see, inside each array, there is another array that gives the item prices. Sometimes there is no price, but that is fine. There shouldn't be on those occasions. So, what I need to do is show the prices as they correspond to the specific item. Here is my code in smarty.

<table>
{foreach name=outer item=row from=$indLine}
<tr>
{foreach key=key item=item from=$row}
<td>{$item}</td>
{foreach key=price item=price from=$row}
<td>{$indLine[4]}</td>
{/foreach}
{/foreach}
</tr>
{/foreach}

My smarty variable for the array is indLine. indLine[4] is where I am trying to access that the prices. I have tried changing that to row, etc. Nothing works. What I get displayed in the browser is what follows.

CCX 4PLY Array Array Array Array SYP Array Array Array Array 4X8 Array Array Array Array Array Array Array Array Array CCX Array Array Array Array SYP Array Array Array Array 4X8 Array Array Array Array Array Array Array Array Array RSH 4-PLY Array Array Array Array SYP Array Array Array Array 4X8 Array Array Array Array Array Array Array Array Array RSH Array Array Array Array SYP Array Array Array Array 4X8 Array Array Array Array Array Array Array Array Array S/F 4-PLY Array Array Array Array SYP Array Array Array Array 4X8 Array Array Array Array Array Array Array Array Array S/F SE Array Array Array Array SYP Array Array Array Array 4X8 Array Array Array Array Array Array Array Array Array

CCX 4PLY    0   SYP 0   4X8 0   Array   0
CCX 0   SYP 0   4X8 0   Array   0
RSH 4-PLY   0   SYP 0   4X8 0   Array   0
RSH 0   SYP 0   4X8 0   Array   0
S/F 4-PLY   0   SYP 0   4X8 0   Array   0
S/F SE  0   SYP 0   4X8 0   Array   0
RSH 3-PLY   0   SYP 0   4X8 0   Array   0
RSH 4-PLY   0   SYP 0   4X8 0   Array   0
RSH 0   SYP 0   4X8 0   Array   0
S/F 4-PLY   0   SYP 0   4X8 0   Array   0

Solution

  • I'm not sure what each value/array represents and what kind of output you expect but try this:

    <table>
    {foreach name=outer item=row from=$indLine}
     <tr>
      {foreach key=key item=item from=$row}
      {if is_array($item)}
      {foreach key=key item=price from=$item}
      <td>{$price}</td>
      {/foreach}
      {else}
      <td>{$item}</td>
      {/endif}
      {/foreach}
     </tr>
    {/foreach}
    

    When you are in foreach loop you use a variable defined by item. I suggest you build the assocc array in php and then use array keys to print the data you want rather than iterating blindly through the array