Reputation: 1392
javascript help: i have a php page that echos
['A', 28.006660938911], ['B', 71.993339061089]
now i need this converted into an array in javascript, but in ajax, "var myData = new Array($http.responseText);" does not work
Upvotes: 1
Views: 1580
Reputation: 725
var myJSONObject = <?php echo json_encode($someArray); ?>
alert( myJSONObject.keyInTheArray )
Upvotes: 0
Reputation: 12235
If given string is:
var s = "['A', 28.006660938911], ['B', 71.993339061089]";
try evaluating it (if you have no access to the PHP code you are using):
var array = eval("[" + s + "]");
try changing the response to JSON format (if you HAVE access to that PHP code):
echo json_encode(array(array('A', 28.006660938911), array('B', 71.993339061089)));
if i am wrong and you are given with two different arrays, try splitting the string first
Upvotes: 3