0

I've Following type of array in smarty..

[Main_array] => Array
(
    [splitlist] => 1
    [counter] => 2
    [listNames] => Array
        (
            [0] => Material
            [1] => Color
        )

    [splittedLists] => Array
        (
            [Material_item_1] => Array
                (
                    [White] => Array
                        ( 
                            [image] => /img/thumbnail.gif
                            [imageLink] => /static/white.html
                        )

                    [Black] => Array
                        (

                            [image] => /img/no-image.gif
                            [imageLink] => /static/black.html
                        )
                )

            [Material_item_2] => Array
                (
                    [Red] => Array
                        (
                            [image] => /img/no-image.gif
                            [imageLink] => /static/Red.html
                        )

                    [Yellow] => Array
                        (
                            [image] => /img/no-image.gif
                            [imageLink] => /static/yellow.html
                        )

                )
        )
)

I want following output from this array

*  Material
    -  Material_item_1
    -  Material_item_2
*  Color
    -  Nude
    -  Black
    -  Red
    -  Yellow

And what i've done so far is as follow..

[{foreach from=$item2.listNames key=subKey1 item=subItem1 name=subLp1}]
   <h2>
       <label>[{$subItem1|replace:"-":" "}]</label>
   </h2>
   <ul style="margin-left:20px;display:block;">
      [{foreach from=$item2.splittedLists key=subKey2 item=subItem2 name=subLp2}]
         <li style="float:none;"><strong>[{$subKey2|replace:"-":" "}]</strong></li>
      [{/foreach}]
   </ul>
[{/foreach}]

And I'm getting....

*  Material
    -   Material_item_1
    -   Material_item_1
    -   Material_item_2
    -   Material_item_2
*  Color
    -   Material_item_1
    -   Material_item_1
    -   Material_item_2
    -   Material_item_2

I'm using PHP - OXID & Smarty. One thing I should mention here is the element of [splittedLists] could be more than or less than 2. But logic for 2 element is also welcome.

UPDATE: I've talked to client and So, Now any suggestion regarding changes in structure are also welcome. Plz help

UPDATE2: Here follows the PHP array.

$arr = array(
    'Main_array' => array
    (
        'splitlist' => 1,
        'counter' => 2,
        'listNames' => array
        (
            '0' => "Material",
            '1' => "Color"
        ),
        'splittedLists' => array
        (
            'Material_item_1' => array
            (
                'White' => array
                ( 
                   'image' => "/img/thumbnail.gif",
                   'imageLink' => "/static/white.html"
                ),
                'Black' => array
                (
                   'image' => "/img/no-image.gif",
                   'imageLink' => "/static/black.html"
                 )
            ),

            'Material_item_2' => array
                (
                    'Red' => array
                        (
                            'image' => "/img/no-image.gif",
                            'imageLink' => "/static/Red.html"
                        ),

                    'Yellow' => array
                        (
                            'image' => "/img/no-image.gif",
                            'imageLink' => "/static/yellow.html"
                        )
                )
        )
)
);
9
  • can you show the array,instead of var_dump?
    – Sibu
    Oct 17, 2012 at 11:43
  • @Sibu This array i put here usign print_r function and I think its enough. Oct 17, 2012 at 12:03
  • i wanted to execute your code, so if can provide array..maybe i will try
    – Sibu
    Oct 18, 2012 at 4:01
  • i want the full array output.. so that i can help :) Oct 18, 2012 at 8:58
  • @Sibu & Vijay: PHP array is added to... Hope this will useful to answer my question.. Thanks Buddies.. Oct 19, 2012 at 7:12

1 Answer 1

1

For array you presented in PHP in Smarty 3.1.18 it should look like this:

{foreach $item2.listNames as $listType}
   <h2>
       <label>{$listType}</label>
   </h2>

   <ul style="margin-left:20px;display:block;">

      {if $listType eq 'Material'}
                {foreach $item2.splittedLists as $k => $v}
                    <li style="float:none;"><strong>{$k}</strong></li>
                {/foreach}
      {elseif $listType eq 'Color'}
               {foreach $item2.splittedLists as $k => $v}
                  {foreach $v as $colour => $versions}
                    <li style="float:none;"><strong>{$colour}</strong></li>
                  {/foreach}
               {/foreach}
      {/if}
   </ul>   
{/foreach}

Output for this will be:

 Material

    Material_item_1
    Material_item_2

Color

    White
    Black
    Red
    Yellow

(Of course no "Nude" is displayed because it wasn't in array)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.