我从数据库中提取了记录并将它们分配给变量$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/