Jump to content

Store php query array in javascript


microsoftdotcom

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...