如何在名稱中有一個短劃線時呈現數組鍵值的值?小枝渲染陣列鍵中有一條破折號
我有這樣的片段:
$snippet = "
{{ one }}
{{ four['five-six'] }}
{{ ['two-three'] }}
";
$data = [
'one' => 1,
'two-three' => '2-3',
'four' => [
'five-six' => '5-6',
],
];
$twig = new \Twig_Environment(new \Twig_Loader_String());
echo $twig->render($snippet, $data);
輸出是
1
5-6
Notice: Array to string conversion in path/twig/twig/lib/Twig/Environment.php(320) : eval()'d code on line 34
而且它呈現four['five-six']
罰款。但在['two-three']
上拋出錯誤。
那是因爲'兩three'被用作局部符號..它像試圖使用'$兩個three'原始的PHP。如果你在你的例子中使用Twig,那麼你應該把數組作爲另一個數組的成員,並且使用變量名作爲關鍵字,比如'$ data = array('values'=> $ theOtherArray);' – prodigitalson 2013-05-06 22:54:33