数据库查询的结果使用以下代码填充数组:
if (is_array($result)) {
$column_count = (int)ceil( count($result) / 2 );
$row_count = (int)0;
$column = 1;
foreach ($result AS $value) {
$array['concordance'][$column][] = [
'concordance_word' => $value['word'],
'book_name' => $value['book_name'],
'chapter' => $value['chapter'],
'verse' => $value['verse'],
];
++$row_count;
if ( $column_count === $row_count ) { ++$column; }
}
}
循环填充后的$ array示例是:
Array
(
[concordance] => Array
(
[1] => Array
(
[0] => Array
(
[concordance_word] => aaron
[book_name] => Micah
[chapter] => 6
[verse] => 4
)
[1] => Array
(
[concordance_word] => aaron
[book_name] => Luke
[chapter] => 1
[verse] => 5
)
[2] => Array
(
[concordance_word] => aaron
[book_name] => Acts
[chapter] => 7
[verse] => 40
)
我不明白为什么以下内容无法访问Twig视图中的变量:
<h3>{{ document.concordance.1.0.concordance_word|capitalize }}</h3>
在我将 $ column 添加到数组之前,该变量是可访问的,如下所示:
<h3>{{ document.concordance.0.concordance_word|capitalize }}</h3>
我没有对控制器进行任何更改。 dump 命令显示传递的变量。 Twig是否将 1 解释为真?是否有一些 |语法我必须申请Twig才能将其视为多维数组变量?
答案 0 :(得分:1)
您可以使用方括号访问数组,如下所示:
<h3>{{ document.concordance[1][0].concordance_word|capitalize }}</h3>