如何在 smarty 中使用 array_merge?

How can I use array_merge in smarty?

我有两个数组 $array1$array2,我想将它们合并到 smarty 模板中以进行进一步处理。我不想在 php 中执行此操作,因为我也必须单独使用它们,如何在 smarty tpl 中合并两个数组?

<?php
// This is effectively the same as assign()
$smarty->append('foo', 'Fred');
// After this line, foo will now be seen as an array in the template
$smarty->append('foo', 'Albert');

$array = array(1 => 'one', 2 => 'two');
$smarty->append('X', $array);
$array2 = array(3 => 'three', 4 => 'four');
// The following line will add a second element to the X array
$smarty->append('X', $array2);

// passing an associative array    
$smarty->append(array('city' => 'Lincoln', 'state' => 'Nebraska'));
?>

您可以通过将两个数组附加到一个新变量来创建一个新变量。

参见文档 here

根据你的安全配置和 smarty 版本,你可以简单地做

{assign 'array_merged' $array1|array_merge:$array2}

有关正确安全设置的更多信息,请查看此 http://www.smarty.net/docs/en/advanced.features.tpl#advanced.features.security

$php_functions is an array of PHP functions that are considered trusted and can be used from within template. To disable access to all PHP functions set $php_functions = null. An empty array ( $php_functions = array() ) will allow all PHP functions. The default is array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array','time','nl2br').