ginerjm Posted April 14, 2011 Share Posted April 14, 2011 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 14, 2011 Share Posted April 14, 2011 When you generate $ar, give a key. For example, $ar[$row["id"]] = $row["name"]; Then use json_encode. JavaScript has objects, which can be treated as associative arrays. So yes, it does. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 14, 2011 Author Share Posted April 14, 2011 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 14, 2011 Share Posted April 14, 2011 Use json_encode() instead of your own creation. var jsgolfers = ; Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 14, 2011 Author Share Posted April 14, 2011 you da man! That was the simplest fix I've ever been given in all my years on many different forums for many different subjects. Thank you very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.