Accepted answer

The d3.json call is asynchronous; the code included after your callback is executed before the "links" variable has been populated.

var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
  if (error) return console.warn(error);
  links = json;

  console.log(links); // your links are populated here

  // include the rest of your app here

// links is still undefined here because the success callback hasn't been run yet


you would need to write links = [{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW,"target":"EA","type":"lie"}]

At that point links will hold that information. You may however want to consider an array.

Related Query