【问题标题】:How can I use array_merge in smarty?如何在 smarty 中使用 array_merge?
【发布时间】:2015-12-10 09:58:41
【问题描述】:

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

【问题讨论】:

  • 取决于你的安全配置和smarty版本,你可以简单的做{assign 'array_merged' $array1|array_merge:$array2}
  • 谢谢@sofl,这是我要找的,你能把它作为答案发布,以便我接受。

标签: php smarty


【解决方案1】:

取决于你的安全配置和 smarty 版本,你可以简单的做

{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 函数。默认为array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array','time','nl2br')。

【讨论】:

    【解决方案2】:
    <?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

    【讨论】:

    • 感谢您的回答,但我真的很想在 Smrty tpl 文件中执行此操作,您还有其他建议吗,这真的可以帮助我解决这个问题。
    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多