PHPLover
PHPLover

Reputation: 13005

How to print the multi dimensional array values in smarty?

I want to print the following array named $all_class_subjects in smarty template:

Array
(
[4] => Array
        (
            [class_id] => 5
            [class_name] => V
            [class_order] => 0
            [class_subject_assign] => 0
            [class_added_date] => 1365837028
            [class_updated_date] => 1365837028
            [class_added_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [class_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [sub_data] => Array
                (
                    [0] => Chemistry
                    [1] => Mathematics
                )

        )
)

And the code which I'm trying to print the array(not all elements only class_name, and subject names present in sub_data array):

{if $all_class_subjects}
         {foreach from=$all_class_subjects item=class_subject_data}
       <tr>
           <td width="20%">{$class_subject_data.class_name|capitalize}</td>
           <td width="40%">
            {foreach from=class_subject_data.sub_data item=subject key=subject_name}

            <b>{$subject.subject_name}</b>
              <br />
            {/foreach}

         {/foreach}     h

           </td>
</tr>

But I'm not able to print the subject names of the sub_data array. So please help we by telling how to print those subject names. Please help me to print only those subject names, other things I'll manage on my own. Thanks in advance.

Upvotes: 0

Views: 2675

Answers (1)

Dave
Dave

Reputation: 892

You may have a syntax error

{foreach from=class_subject_data.sub_data item=subject key=subject_name}

should be (the $ appears to be missing on class_subject_data.sub_data):

{foreach from=$class_subject_data.sub_data item=subject key=subject_name}

Failing that just under the foreach loop use {$subject|@print_r} to see if you're getting any data. And just before the loop, see if you're getting any data in your multi dimensional array {$class_subject_data.sub_data|@print_r}.

When you debug it and everything looks to be working, to print the values, use the following. from is the data array, item is the value of the array and key is the key of the array. Hope this helps!

{foreach from=$class_subject_data.sub_data item=subject_name key=subject_key}
    The subject is: {$subject_name} and the Key is {$subject_key}

A variable in smarty is defined by {$variable_name}. See the docs for more info.

Upvotes: 2

Related Questions