dongqie2010 2011-03-27 02:25
浏览 114
已采纳

将数组作为自定义函数中的参数传递

Hey, I was wondering if it was possible to pass an associative array as a parameter in a custom function. This is my scenario:

In the php file I set the array:

$dataArr = array('one'=>'1','two'=>'2','three'=>'3');
$tpl->assign('dataArr',$dataArr);

This is my custom function dulled down

  function smarty_function_drawChart($params, &$smarty){
            print_r($params);
    }

This is my function call in the template

{drawChart data={$dataArr} title='Title of the Chart'}

The problem I am having is that if you notice where I print_r($params), that shows:

Array
(
    [data] => Array
    [title] => Title of the Chart
)

It seems to be passing the string 'Array' rather than the actual array. I have done debugging right before passing the $dataArr that shows {$dataArr.one} has a value. Once inside my custom function $params['data'].one does not exist.

Any ideas on what I am doing wrong?

Thanks
Levi

  • 写回答

1条回答 默认 最新

  • dtc4547 2011-03-27 16:35
    关注

    I am still not 100% sure why my code above didn't work. My thought is that the brackets work just as an 'echo' would do in php, which is why the string 'Array' was being passed into the function. I was able to get it to work by simple removing the brackets around the $dataArr variable.

    This was my original call:

    {drawChart data={$dataArr} title='Title of the Chart'}
    

    This is my new call that works

    {drawChart data=$dataArr title='Title of the Chart'}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料