Search code examples
javascriptphpcanvasjs

PHP array data convert to JS ( Canvas JS )


I am trying to make a graph of my own data (JS)which I have stored in a php array

Now I have to make some kind of loop to make(Javascript)

  dataPoints: [

              { x: 10, y: 10 },
              { x: 20, y: 15 },
              { x: 30, y: 25 },
              { x: 40, y: 30 },
              { x: 50, y: 28 }
              ]

Needs to be something like

 {x: $arraytime[0], y:arraycloud[0]) , 
 {x: $arraytime[1], y:arraycloud[1]) , 
 {x: $arraytime[2], y:arraycloud[2]) , 

etcetera.

I have no clue how to do this


Solution

  • You can use a json_encode for this, if you prepare the array in php, like this:

    $arr = array();
    for ($i = 0; $i < count($arraytime); $i++) {
        $arr[] = array('x' => $arraytime[$i], 'y' => $arraytime[$i]);
    }
    
    echo json_encode($arr);