我想調用smarty模板中的foreach循環內部的函數,如果有人在smarty模板中熟知它是函數,請幫我解決這個問題。如何調用Smarty Foreach Loop中的函數?
實施例在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}
在此先感謝。
我不建議你把這樣的功能裏面Smarty的...你的PHP裏面更好的只是準備要輸出和智者的結果只會顯示它。 – Svetoslav 2013-04-30 06:10:56
所以PHP函數不可能在smarty模板中調用? – 2013-04-30 06:33:06