PHP结果不会显示在smarty模板中

时间:2011-10-12 07:59:21

标签: php foreach smarty

我有一个变量$my_arr,其var_dump($my_arr)值为

array(3) { [0]=> object(stdClass)#6 (9) { ["prod_index"]=> string(1) "1" ["prod_code"]=> string(4) "amp1" ["prod_name"]=> string(11) "Amplifier A" ["prod_price"]=> string(6) "100.00" ["frequency"]=> string(1) "2" ["gain"]=> string(1) "2" ["gain_variation"]=> string(3) "0.2" ["noise"]=> string(1) "2" ["power"]=> string(2) "10" } [1]=> object(stdClass)#7 (9) { ["prod_index"]=> string(1) "3" ["prod_code"]=> string(4) "amp1" ["prod_name"]=> string(13) "Amplifire1.33" ["prod_price"]=> string(6) "133.00" ["frequency"]=> string(4) "2.12" ["gain"]=> string(1) "2" ["gain_variation"]=> string(3) "0.2" ["noise"]=> string(1) "2" ["power"]=> string(2) "10" } [2]=> object(stdClass)#8 (9) { ["prod_index"]=> string(1) "4" ["prod_code"]=> string(4) "amp1" ["prod_name"]=> string(13) "Amplifier1.45" ["prod_price"]=> string(6) "150.00" ["frequency"]=> string(4) "2.99" ["gain"]=> string(1) "2" ["gain_variation"]=> string(3) "0.2" ["noise"]=> string(1) "2" ["power"]=> string(2) "10" } } 

我很聪明地试过

{foreach name = fe1 item = k from = arr}
{$k->prod_index}    
{/foreach}

但它没有显示任何结果。

1 个答案:

答案 0 :(得分:0)

假设您已经将数组分配给smarty,如:

$smarty->assign( "arr", $my_arr );

你需要使用“。”语法,如:

{foreach name = fe1 item = k from = arr}
{$k.prod_index}    
{/foreach}