数据库查询的结果用以下代码填充数组:
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看到这是一个多维数组变量?
可以用方括号访问数组,如下所示:
<h3>{{ document.concordance[1][0].concordance_word|capitalize }}</h3>