【问题标题】:How to access multidimensional array element in smarty template?如何访问 smarty 模板中的多维数组元素?
【发布时间】:2013-09-20 14:16:18
【问题描述】:

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

$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}

【问题讨论】:

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


    【解决方案1】:

    这将适用于访问内部数组键元素和包含它的子序列数组。

    <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>
    

    【讨论】:

      【解决方案2】:

      我建议您使用两个聪明的“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}
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 2023-03-11
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多