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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.