检查array中是否存在key


Check if key exists in smarty array

我有一个像这样的数组:

array(
    'name1' => array('city1', 'city2', 'city3'),
    'name2' => array('city1', 'city4'),
    'namen' => array('city1', 'city7', 'cityn')
);

作为:$my_names传递给smarty在我的Smarty模板中有一个循环,看起来像这样:

{{foreach from=$names item=name}}
  {{foreach from=$cities item=city}}
  //Check if name1 exist and after check if the city is in the array for that name
  {{/foreach}}
{{/foreach}}

我不知道如何使用$name$city来访问数组$my_names

我试过做if($my_names.$name.$city),但它不工作。

这个符号也可以用:

{if 'needle'|array_key_exists:$haystack}
....
{/if}

可以使用array_key_exists()函数检查键是否在数组中

{if array_key_exists('needle', $haystack)} 
    DoSomethingAboutIt 
{/if}