0

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
2
  • Show what output you expect. Commented Dec 31, 2014 at 17:19
  • It needs to be something similar to this #2 S4S 2X6 SYP $679 $674 $669 $689 Commented Dec 31, 2014 at 17:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

That changed the display to the browser somewhat, but still not doing what I need it to do. It still displays "Array" rather than showing the array elements.
I edited the answer in the meantime. You need to check if the item you are printing is an array and iterate over it again if it is.
That done it with one exception. On this line (foreach key=price item=price from=$item), I had to change the key=price to key=key. Thank you for pointing me in the right direction. I am most definitely marking your answer as correct.
Oh right, you can't have key and item vars named the same, sorry. I edited the post. Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.