首页 > 解决方案 > 如何在 smarty 中获取数组的特殊名称的值?

问题描述

我有一个如下的 PHP 数组:

$list = array(
    array(
        'name'=>'switch',
        'value'=>'cisco'
    ),
    array(
        'name'=>'switchport',
        'value'=>'2'
    )
);

如何一步获得name='switch'or的物品?name='switchport'

因为这是在smarty模板中,所以有 img

<img src="http://localhost:8080/api.php?switch={$my_need_switch_value}&switchport={$my_need_switchport_value}">

my_need_switch_value那么,我怎样才能$list一步到位呢?

标签: phpsmarty

解决方案


您可以使用{assign}来分配新变量。

{foreach from=$list item=customfield}
    {if $customfield.name=='switch'}
        {assign var=switch value=$customfield.value}
    {elseif $customfield.name=='switchport'}
        {assign var=portname value=$customfield.value}
    {/if}

{/foreach}

然后你可以直接使用$switchand $switchport


推荐阅读