Search code examples
phparrayssmartywhmcs

Smarty Undefiend Variable As Array


I have a smarty application that was written about 1 year ago , now i migrated to Smarty v3 and i've got some errors in my code that i dont have any clue how to fix them. here i will list them , thanks

1-Cannot use object of type Smarty_Undefined_Variable as array

$this->_tpl_vars['cart']['domains'] = array();

i was able to set variable like this but now i'm getting error with this code , i read the Smarty document and it said : for making variables i should use this code :

$template->assign('myNewVariable', 'myNewValue');

the problem is that i want to add an array in variable and its nested like the code i have shown above and i need something like this as array :

 $template->assign('cart[domains]', 'myNewValue');

or maybe :

$test= $template->assign('cart', array);
$domain=$test->assign('domains',array);

both of them should be assigned as array.


Solution

  • If you want to an array to a variable, you can do like this

    $arr['domain']=YourNewValue;
    $template->assign('cart', $arr);
    

    Simple as that.