php - 如何访问 Moodle 中 Mustache 模板中对象数组的值?

标签 php moodle mustache

我从数据库中提取了记录并将它们分配给变量$results,我试图循环访问这个对象数组,但无法访问对象中的任何内容,并且它只运行一次尽管有 3 个对象。

这是我从 var_dump($results); 得到的内容:

array(3) 
{ 
  [3]=> object(stdClass)#261 (3) { 
    ["id"]=> string(1) "3" ["grade"]=> string(1) "A" ["score"]=> string(2) "95" } 
  [2]=> object(stdClass)#260 (3) { 
    ["id"]=> string(1) "2" ["grade"]=> string(1) "A" ["score"]=> string(2) "90" } 
  [1]=> object(stdClass)#259 (3) { 
    ["id"]=> string(1) "1" ["grade"]=> string(1) "C" ["score"]=> string(2) "50" } 
}

这是我在 mustache 模板中尝试的内容:

      <table>
        <tr>
          <th>Score</th>
          <th>Grade</th>
        </tr>
        {{#results}}
          <tr>
            <td>{{ score }}</td>
            <td>{{ grade }}</td>
          </tr>
        {{/results}}
      </table>

最佳答案

我看到的问题是 $results 数组的键不是从 0 开始的连续值,因此它被视为具有特定成员值的对象,并且无法在 Mustache 中循环。

如果你想在 Mustache 中循环它,那么调用:

$results = array_values($results);

在将其传递到 Mustache 模板之前 - 之后,我认为它应该可以工作。

关于php - 如何访问 Moodle 中 Mustache 模板中对象数组的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71221872/

相关文章:

php - 我无法在 Ubuntu 的 php 脚本中终止进程

javascript - mustache js语法调用带参数的函数?

java - Spring REST 文档抛出 MustacheException

mysql - 比较两个表并删除与两列比较的公共(public)行

javascript - 编辑模式下的 HTML 表格自动刷新问题

javascript - 从 JS 传递变量并在 PHP 中使用它不起作用

php - PHP 中的警报输入名称

php - 在一个简单的 CMS 上工作——如何处理用户的访问权限?

php - 从php数组中提取数据

php - Moodle enrol_manual_enrol_users 不起作用