James Gu
James Gu

Reputation: 1392

make a Javascript array from php echo output

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

Answers (2)

zion ben yacov
zion ben yacov

Reputation: 725

var myJSONObject = <?php echo json_encode($someArray); ?>
alert(  myJSONObject.keyInTheArray  )

Upvotes: 0

shybovycha
shybovycha

Reputation: 12235

If given string is:

var s = "['A', 28.006660938911], ['B', 71.993339061089]";
  1. try evaluating it (if you have no access to the PHP code you are using):

    var array = eval("[" + s + "]");
    
  2. 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)));
    
  3. if i am wrong and you are given with two different arrays, try splitting the string first

Upvotes: 3

Related Questions