我怎样才能在smarty中使用array_merge?

时间:2015-12-10 09:58:41

标签: php smarty

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

2 个答案:

答案 0 :(得分:7)

取决于您的安全配置和智能版本,您可以轻松完成

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

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

  

$ php_functions是一系列被认为可信的PHP函数,可以在模板中使用。要禁用对所有PHP函数的访问,请设置$ php_functions = null。空数组($ php_functions = array())将允许所有PHP函数。默认为数组(' isset','空','计数',' sizeof',' in_array', ' is_array''时间'' nl2br'。)

答案 1 :(得分:1)

<?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