我想在 smarty 模板中调用 foreach 循环内的函数,如果有人在 smarty 模板和它的函数中很熟悉,请帮助我解决这个问题。
PHP 中的示例:
<?php
function array_combine_($keys, $values)
{
$result = array();
foreach ($keys as $i => $k) {
$result[$k][] = $values[$i];
}
array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
return $result;
}
$country = array("India","United States","United Kingdom","India","Australia");
$city = array("Coimbatore","Newyork","London","Bangalore","Melborne");
$output = array_combine_($country, $city);
$get_array_keys = array_keys($output);
$get_array_values = array_values($output);
$get_count = count(array_keys($output));
for($i=0;$i<$get_count;$i++){
if(is_array($get_array_values[$i])){
echo "<b>".$get_array_keys[$i]."</b> - ".implode(",",$get_array_values[$i])."<br>";
}
else{
echo "<b>".$get_array_keys[$i]."</b> - ".$get_array_values[$i]."<br>";
}
}
?>
输出 :
India - Coimbatore,Bangalore
United States - Newyork
United Kingdom - London
Australia - Melborne
Smarty 中的示例:
{foreach from=$companies item=record key=key}
{$record.country}
{$record.city}
{/foreach}
现在如何将这个 {$record.country} 和 {$record.city} 值传递给 array_combine_($arg1,$arg2); 在 {foreach} 循环内,如下所示:
{foreach from=$companies item=record key=key}
{$record.country}
{$record.city}
array_combine_({$record.country},{$record.city});
{/foreach}
提前致谢。