2013-05-07 120 views
1

我有一個名爲$cs_map_data在PHP文件以下多維數組,我已經將其分配到Smarty模板:如何從smarty中的多維數組訪問密鑰?

Array 
(
    [2] => Array 
     (
      [class_id] => 2 
      [class_name] => II 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 81 
          [subject_name] => 11 Engllish 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [3] => Array 
     (
      [class_id] => 3 
      [class_name] => III 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 155 
          [subject_name] => Hidi 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 156 
          [subject_name] => 11 Maths 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [2] => Array 
         (
          [cs_map_id] => 157 
          [subject_name] => 11 Science 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [3] => Array 
         (
          [cs_map_id] => 158 
          [subject_name] => 11 Engllish 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [4] => Array 
     (
      [class_id] => 4 
      [class_name] => IV 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 86 
          [subject_name] => Physics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [6] => Array 
     (
      [class_id] => 6 
      [class_name] => VI 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 144 
          [subject_name] => Mathematics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 145 
          [subject_name] => dfadadadsagfasrsarasrarBiology 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [7] => Array 
     (
      [class_id] => 7 
      [class_name] => VII 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 129 
          [subject_name] => Physics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 130 
          [subject_name] => Chemistry11 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [2] => Array 
         (
          [cs_map_id] => 131 
          [subject_name] => 11 Science 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [8] => Array 
     (
      [class_id] => 8 
      [class_name] => VIII 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 67 
          [subject_name] => Hidi 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 68 
          [subject_name] => 11 Engllish 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

    [9] => Array 
     (
      [class_id] => 9 
      [class_name] => IX 
      [class_checked] => 0 
      [class_subjects] => Array 
       (
        [0] => Array 
         (
          [cs_map_id] => 87 
          [subject_name] => Mathematics 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [1] => Array 
         (
          [cs_map_id] => 88 
          [subject_name] => Hidi 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

        [2] => Array 
         (
          [cs_map_id] => 89 
          [subject_name] => 11 Science 
          [subject_checked] => 0 
          [teacher_cs_id] => 
         ) 

       ) 

     ) 

) 

現在我要訪問鍵[cs_map_id]的價值和[subject_name]每個元素的在聰明的模板。我嘗試使用foreach,但無法獲得密鑰[cs_map_id][subject_name]的值。 任何人都可以幫助我如何訪問smarty中foreach循環中的密鑰[cs_map_id][subject_name]的值。本人在智者的模板寫 代碼如下:

<table>       
       <tr> 
       {assign var='i' value=0} 
       {if $cs_map_data} 
       {foreach from=$cs_map_data item="map_data"} 
       {if $i%4 == 0}</tr><tr>{/if} 
       <td align="left" valign="top" width="150"> 

        <input type="checkbox" name="cs_map_id[]" id="{$map_data.cs_map_id}" value="{$map_data.cs_map_id}" onChange="get_test_by_category('{$map_data.cs_map_id}'); return false;" /> 
       {$map_data.subject_name} 
       </td> 
       {assign var='i' value=$i+1} 
       {/foreach} 
       {else} 
         <td> 
          <i>No Subject is assigned to the class.</i> 
        </td> 
       {/if} 
       </tr> 
       </table> 

任何一個可以幫助我如何在forech在智者模板中使用兩個關鍵的價值觀? 在此先感謝。

+0

你在使用Smarty 2還是3? – eidsonator 2013-05-07 14:23:59

+0

@eidsonator Smarty 2 – PHPLover 2013-05-07 14:27:24

+0

啊,這就是我從你的foreach語法中想到的。我對3更加熟悉......並且在2和3之間的這個區域有一些大的變化。但是我認爲你想用一個{SECTION}而不是一個foreach,並且你必須將它們嵌套到遍歷「內部」數組。 – eidsonator 2013-05-07 14:31:32

回答

3

這將在Smarty 2和3中工作。{foreach}在版本之間有很大變化,但{section}的工作原理相同。

{section name=map loop=$cs_map_data} 
    <tr> 
     <td>{$cs_map_data[map].class_name}</td> //this is the format to access your data from the array 
    {section name=subject loop=$cs_map_data[map].class_subjects} 
      <td> 
       {$cs_map_data[map].class_subjects[subject].subject_name} //this is how to access the entries of your 'inner' array 
      </td> 
    {/section} //end your nested section 
    </tr> 
{/section} //end your outer section 

應通過你的陣列的工作循環......

的部分Smarty的手冊:http://www.smarty.net/docsv2/en/language.function.section.tpl

祝你好運!

+0

非常感謝。你的代碼爲我做了必要的魔術。再次感謝你挽救我的生命。 – PHPLover 2013-05-07 15:13:54

+1

當我第一次開始學習聰明的時候,花了兩天時間來解決這個問題!這很複雜,但是當你將頭部纏繞在頭上時,它非常簡單。祝你的項目好運! – eidsonator 2013-05-07 15:17:36