php Smarty:检查变量是否在数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8093148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:57:27  来源:igfitidea点击:

Smarty: check if variable is in array

phparrayssmarty

提问by Aleks G

I'm using php with smarty. In php I have two arrays:

我正在使用带有 smarty 的 php。在 php 中,我有两个数组:

$code = Array
(
    [n_id] => 1
    [t_code] => ABC123
    [t_description] => Test code
    [b_enabled] => Yes
    [n_type] => 3
    [dt_start] => 
    [dt_end] => 
    [n_min_req_gbp] => 0
    [n_min_req_usd] => 0
    [n_amount_gbp] => 
    [n_amount_usd] => 
    [n_max_overall_gbp] => 
    [n_max_overall_usd] => 
    [n_extra] => 6
    [b_reuse] => No
    [n_applications] => Array
        (
            [0] => 2
        )
)

and

$all_application = Array
(
    [1] => New registration
    [2] => Mid-subscription upgrade
    [3] => Subscription renewal
    [4] => Additional purchase
)

Note that the second array may - and will - grow, this is the reference data, from which n_applicationsarray field in the first array is built. That is, the array in n_applicationswill contain a subset of keys from the $all_applicationsarrays.

请注意,第二个数组可能 - 而且将会 - 增长,这是参考数据,从中n_applications构建第一个数组中的数组字段。也就是说,数组 inn_applications将包含数组中键的子集$all_applications

Now, I'm assigning these two arrays into the template:

现在,我将这两个数组分配到模板中:

$template->assign('code', $code);
$template->assign('apps', $all_applications);

And in the template, I'm creating a form for editing the fields in the $codearray. Everything is working fine except the 'applications' selection. I want to pre-select those apps that are already in the n_applicationsfield. So, in my template I have this:

在模板中,我创建了一个表单来编辑$code数组中的字段。除了“应用程序”选择外,一切正常。我想预先选择那些已经在n_applications现场使用的应用程序。所以,在我的模板中,我有这个:

<select name="c_apps[]" size="3" class="multiselect" multiple="multiple">
    {foreach from=$apps key=k item=a}
        {assign var=v value=$k|@array_search:$code['n_applications']}
        <option value="{$k}"{if $v!==FALSE} selected="selected"{/if}>{$a|escape}</option>
    {/foreach}
</select>

However this doesn't work as expected - and ALL options end up being selected. I tried using in_arrayfunction - but with the same result. What's the best way to achieve what I'm after?

然而,这并没有像预期的那样工作 - 并且最终选择了所有选项。我尝试使用in_array函数 - 但结果相同。实现我所追求的最佳方式是什么?

采纳答案by Aleks G

After a bit of struggling in all possible directions, I finally managed to pull it off like this (smarty code only)

经过在所有可能的方向上的一些挣扎,我终于设法像这样成功(仅限智能代码)

<select name="c_apps[]" size="3" class="multiselect" multiple="multiple">
    {foreach from=$apps key=k item=a}
        {if @in_array($k, $code.n_applications)}
            {assign var=v value=true}
        {else}
            {assign var=v value=false}
        {/if}
        <option value="{$k}"{if $v} selected="selected"{/if}>{$a|escape}</option>
    {/foreach}
</select>

And this did the trick.

这成功了。

回答by Vilius Paulauskas

You can do it like this:

你可以这样做:

<select name="c_apps[]" size="3" class="multiselect" multiple="multiple">
    {foreach from=$apps key=k item=a}
        <option value="{$k}"{if in_array($k, $code.n_applications)} selected="selected"{/if}>{$a|escape}</option>
    {/foreach}
</select>

回答by Bjoern

I've done something similar a few years back, and stumbled over the same logical challenge.

几年前我做过类似的事情,并偶然发现了同样的逻辑挑战。

My solution was to modify the base array (in your case, $all_applications) while adding another key there (maybe ['opt_selected']). I left the default value empty, and for the data I wanted to have selected, I've changed the value to, guess what, ... selected="selected".

我的解决方案是修改基本数组(在你的情况下,$all_applications),同时在那里添加另一个键(也许['opt_selected'])。我离开的默认值清空,并为数据我想有选择的,我已经改变了价值,你猜怎么着,... selected="selected"

This makes it rather easy for your Smarty template:

这使您的 Smarty 模板变得相当容易:

<option value="{$k}" {$a.opt_selected|default:''}>{$a|escape}</option>

It might not be the best solution, but it helps leaving alot of code out of the template where I usually don't want too much program logic.

它可能不是最好的解决方案,但它有助于将大量代码从我通常不想要太多程序逻辑的模板中删除。

Update

更新

To counter having the HTML part in your php code, you might as well just flag the array:

为了防止在你的 php 代码中包含 HTML 部分,你最好只标记数组:

$all_applications['opt_selected'] = 1

...and then arrange Smarty like this:

...然后像这样安排Smarty:

<option value="{$k}" {if $a.opt_selected eq '1'}selected="selected"{/if}>
  {$a|escape}
</option>