microsoftdotcom Posted January 2, 2016 Share Posted January 2, 2016 I cant store my php array in javascript. Been trying to fix the problem for a few days, but It keeps saying that my json_encode is undefined. I hope someone can help me with this. This is my array in php: {{$counter;}} {{$memberArray = array();}} {{$query = \IPS\Db::i()->select( 'name', 'core_members', 'member_group_id = 6 or member_group_id = 4 or member_group_id = 3');}} {{foreach $query as $name}} {{$counter++;}} {{$memberArray[$name] = $counter;}} {{endforeach}} var_dumping the array gives me this: array(4) { ["Robert"]=> int(1) ["Peter"]=> int(2) ["William"]=> int(3) ["Edward"]=> int(4) } This is the code I use to store php variable in javascript: var ar = json_encode($memberArray); But when I loop the array in javascript it says my json_encode is undefined. I dont know what I'm doing wrong Ideally I would like my php array to be stored in a way so that I can call it like this in Javascript: ar['Peter'] returns me 1 Link to comment Share on other sites More sharing options...
microsoftdotcom Posted January 3, 2016 Author Share Posted January 3, 2016 bumping this, since I can get this to work with native php but not in IPB where php syntaxes are slighty changed. Need help Link to comment Share on other sites More sharing options...
Marcher Technologies Posted January 4, 2016 Share Posted January 4, 2016 Because the template engine isn't actually parsing that php, the js thinks it is a javascript function to call, hence the error. This should work: var ar = {expression="json_encode($memberArray)" raw="1"}; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.