Jump to content

php associative array to JS?


ginerjm

Recommended Posts

After much research and some great tips, I have managed to move a php generated query/array to a js array.  My code works great and all but what I need to do now is to make the js array an "associated" one.

 

Ex.

my php query produces a result of

1 Tiger

4 Phil

3 Nick

 

I can make an assoc array in php that would look like:

$ar["1"] = "Tiger"

$ar["4"] = "Phil"

 

instead of

$ar[0] = "Tiger"

$ar[1] = "Phil"

 

How can I get the assoc php array format into a js array so that I can do a look up on a value of "4" and come up with "Phil" instead of undefined?  Does JS even have associative arrays?

Link to comment
https://forums.phpfreaks.com/topic/233762-php-associative-array-to-js/
Share on other sites

ok - I have an assoc php array that works just like I want it to.  The indices of it are not 0.....n but instead the assigned numbers I have in my sql table.

 

I use this to build my js array

var jsgolfers = ["<?php echo implode("\",\"",$golfers); ?>"];

 

which builds just a normal enumerated array, even tho the indices of $golfers are not enumerated.

 

So - while php tells me that $ar["4"] (which is actually the 2nd element) is "Tiger", JS says it is a couple more cells down the line and shows "Phil".

 

Any way of managing this in JS?

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.