如何在smarty模板中访问多维数组元素?

时间:2013-09-20 14:16:19

标签: php arrays multidimensional-array smarty associative-array

我正在从PHP中为smarty分配一个名为$ user_details的多维数组,如下所示:

$smarty->assign('user_details', $user_details);

实际数组 $ user_details 如下所示:

Array
(
    [user_id] => 263129476e186da1dc28c8d0b5e48521
    [user_first_name] => Nishant
    [user_last_name] => Dey
    [user_name] => agridipankar@in.com
    [user_password] => 1994nishant
    [user_email] => agridipankar@in.com
    [user_dob] => 
    [user_subscription] => lifetime
    [user_reg_date] => 18/09/2012 04:09:11 pm
    [user_last_login] => 1351274390
    [user_last_logged_in] => 26/10/2012 11:29:50 pm
    [user_mobile_number] => 9436525368
    [assigned_tests_data] => Array
        (
            [0] => Array
                (
                    [test_name] => JEE XI Test : Mathematics Full Syllabus 2
                    [test_no_questions] => 100
                    [test_max_score] => 400.000
                    [test_duration] => 7200
                )

            [1] => Array
                (
                    [test_name] => JEE XI Test : Mathematics Full Syllabus 1
                    [test_no_questions] => 100
                    [test_max_score] => 400.000
                    [test_duration] => 7200
                )

            [2] => Array
                (
                    [test_name] => JEE XI Test : Probability
                    [test_no_questions] => 50
                    [test_max_score] => 200.000
                    [test_duration] => 3600
                )

            [3] => Array
                (
                    [test_name] => JEE XI Testlet : Probability 2
                    [test_no_questions] => 15
                    [test_max_score] => 60.000
                    [test_duration] => 1200
                )

            [4] => Array
                (
                    [test_name] => JEE XI Testlet : Probability 1
                    [test_no_questions] => 15
                    [test_max_score] => 60.000
                    [test_duration] => 1200
                )
)
)

现在要在smarty模板中打印数组我做了以下代码,但无法打印内部数组值。你能解释一下我哪里出错了吗?提前谢谢。

{section name=users loop=$user_details}
      <table width="100%"  class="base-table tbl-practice" cellspacing="0" cellpadding="0" border="0">
        <thead> 
          <tr>
            <th width="40%" class="sorter-false" style="text-align:center;">Test Name</th>
            <th width="20%" class="sorter-false" style="text-align:center;">No. of Questions</th>
            <th width="20%" class="sorter-false" style="text-align:center;">Total Marks</th>
            <th width="20%" class="sorter-false" style="text-align:center;">Duration</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td valign="top">{$user_details[users].[assigned_tests_data].test_name}</td>
            <td align="center" valign="top">{$user_details[users].[assigned_tests_data].test_no_questions}</td>
            <td align="center" valign="top">{$user_details[users].[assigned_tests_data].test_max_score}</td>
            <td align="center" valign="top">{$user_details[users].[assigned_tests_data].test_duration}&nbsp;Hrs</td>

          </tr>   
            {/section}

2 个答案:

答案 0 :(得分:0)

我建议你使用两个聪明的“foreach”语句而不是一节。例如

{foreach from=$user_details item=users}
<table width="100%"  class="base-table tbl-practice" cellspacing="0" cellpadding="0" border="0">
    <thead> 
      <tr>
        <th width="40%" class="sorter-false" style="text-align:center;">Test Name</th>
        <th width="20%" class="sorter-false" style="text-align:center;">No. of Questions</th>
        <th width="20%" class="sorter-false" style="text-align:center;">Total Marks</th>
        <th width="20%" class="sorter-false" style="text-align:center;">Duration</th>
      </tr>
    </thead>
    <tbody>
   <tr>
  {foreach item=$users.assigned_tests_data item=atd}
    <td valign="top">{$atd.test_name}</td>
    <td align="center" valign="top">{$atd.test_no_questions}</td>
    <td align="center" valign="top">{$atd.test_max_score}</td>
    <td align="center" valign="top">{$atd.test_duration}&nbsp;Hrs</td>        
  {/foreach}
  </tr>
{/foreach}

希望这有帮助。

答案 1 :(得分:0)

这将用于访问内部数组键元素,后续数组包含它。

<table width="100%"  class="base-table" cellspacing="0" cellpadding="0" border="0" id="users_test_listing">
        <thead> 
          <tr>
            <th width="40%" class="sorter-false" style="text-align:center;">Test Name</th>
            <th width="20%" class="sorter-false" style="text-align:center;">No. of Questions</th>
            <th width="20%" class="sorter-false" style="text-align:center;">Total Marks</th>
            <th width="20%" class="sorter-false" style="text-align:center;">Duration</th>
          </tr>
        </thead>
        <tbody> 
          {foreach from=$user_details.assigned_tests_data item=test_data}
          <tr>
            <td valign="top">{$test_data.test_name}</td>
            <td align="center" valign="top">{$test_data.test_no_questions}</td>
            <td align="center" valign="top">{$test_data.Marks}</td>
            <td align="center" valign="top">{$test_data.Time}&nbsp;Hrs</td>
          </tr>   
          {/foreach}
        </tbody>
      </table>