0

I am trying to visualize the data from the currencylayer API.
I am trying to use this as an example: http://bl.ocks.org/Caged/6476579

Here is my php code:

<?php
$response = json_decode(file_get_contents("http://apilayer.net/api/live?access_key=xxxxxxxxxxx&format=1", true));

$keys_array = array();
$values_array = array();
$arra = array();
foreach($response->quotes as $key => $value) {
//    echo "$key is at $value\n";
//      array_push($keys_array, substr($key,3));
//    array_push($values_array,$value);

    array_push($arra,{"letter":substr($key,3),"frequency":$value});
}
print_r($arra);
?>

I am trying to achieve the data in the form of:

[
  {letter: "A", frequency: .08167},
  {letter: "B", frequency: .01492},
  {letter: "C", frequency: .02780},
  {letter: "D", frequency: .04253},
  {letter: "E", frequency: .12702},
  {letter: "F", frequency: .02288},
  {letter: "G", frequency: .02022},
  {letter: "H", frequency: .06094},
  {letter: "I", frequency: .06973},
  {letter: "J", frequency: .00153},
  {letter: "K", frequency: .00747},
  {letter: "L", frequency: .04025},
  {letter: "M", frequency: .02517},
  {letter: "N", frequency: .06749},
  {letter: "O", frequency: .07507},
  {letter: "P", frequency: .01929},
  {letter: "Q", frequency: .00098},
  {letter: "R", frequency: .05987},
  {letter: "S", frequency: .06333},
  {letter: "T", frequency: .09056},
  {letter: "U", frequency: .02758},
  {letter: "V", frequency: .01037},
  {letter: "W", frequency: .02465},
  {letter: "X", frequency: .00150},
  {letter: "Y", frequency: .01971},
  {letter: "Z", frequency: .00074}
]

If I get the values in the above format then I can assign the php variable to the d3 datavariable and visualize accordingly.

The above php code is giving error as follows:

Parse error: syntax error, unexpected '{' in file.php on line 18

Kindly, help me in fixing the error and assignment of the variable to the d3.

2 Answers 2

1

Your missing JSON with PHP, JSON is an javascript object anotation and it won't work in php. You need to use arrays in php and then encode them as json.

<?php
$response = json_decode(file_get_contents("http://apilayer.net/api/live?access_key=xxxxxxxxxxx&format=1", true));

$keys_array = array();
$values_array = array();
$arra = array();
foreach($response->quotes as $key => $value) {
//    echo "$key is at $value\n";
//      array_push($keys_array, substr($key,3));
//    array_push($values_array,$value);

    $arra[] = [
        "letter" => substr($key,3),
        "frequency" => $value
    ];
}
print_r($arra);

// echo json_encode($arra); // to convert the array to json 
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Wow! this worked... BUt is it possible to assign the variable to the javascript d3?
After encoding will it be possible to use it in the javascript.. please enlighten
yes, see JSON.parse()
I just wanted to assign the values as it is. Hence converted the array to json and then tried to assign to javascript variable: var data = <?php echo $jsonvalues ?>; But not working. :(
1

Just change your line from

array_push($arra,{"letter":substr($key,3),"frequency":$value});

to

array_push($arra,array("letter"=>substr($key,3),"frequency"$value));

Since i dont have the conent of that file so can't predict wether it can fix the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.