简体   繁体   中英

PHP D3 combination for visualizing data

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 data variable 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.

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 
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM