Invision Community 4: SEO, prepare for v5 and dormant account notifications Matt November 11, 2024Nov 11
Posted January 2, 20169 yr 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 Edited January 2, 20169 yr by microsoftdotcom
January 3, 20169 yr Author bumping this, since I can get this to work with native php but not in IPB where php syntaxes are slighty changed. Need help
January 4, 20169 yr 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"};
Archived
This topic is now archived and is closed to further replies.