1

how to run loop for php array in javascript( google graph api) i have x and y values in seprate php array and i am printing it like below.

   google.load("visualization", "1", {packages:["corechart"]});
   google.setOnLoadCallback(drawChart);
  function drawChart() {
   var data = google.visualization.arrayToDataTable([

  ['x', 'y'],
  <?php   

for($i=1;$i<1000;$i++)
 {
  ?>
      [<?php echo $x[$i]; ?>,<?php echo $y[$i]; ?>],


  <?php } ?>

 ]);

var options = {
  title: 'CGR plot',
  hAxis: {title: '(0,0)A (800,0) T', minValue: 0, maxValue: 800},
  vAxis: {title: '(800,800)G ;(0,800)C', minValue: 0, maxValue: 800},
  legend: 'none',
  pointSize: 1,
        };

var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

chart.draw(data, options);
 }
  </script>

    <div id="chart_div" style="width: 800px; height: 500px;"></div>

and it is working fine ...but here loop is runnning for 1000 times and try to run it according to array size using sizeof($x) or count($x). but it is not working. do u have any suggestion for this

5
  • for($i=1;$i<1000;$i++) makes loop running 1000 times, instead put for($i=1;$i<count($x);$i++) Commented Nov 17, 2014 at 12:01
  • cant you just use for($i=1;$i<count($x);$i++) ? Commented Nov 17, 2014 at 12:02
  • 1
    you beat me to it by second :) Commented Nov 17, 2014 at 12:02
  • P.S. array indexing starts from '0' .. Commented Nov 17, 2014 at 12:02
  • that's what i wrote , if i am using count or sizeof then it is not working. i.e for($i=0;$i<count($x);$i++) Commented Nov 17, 2014 at 12:10

1 Answer 1

1

Use json_encode instead of looping though the array

echo json_encode($array);

in this case:

var data = google.visualization.arrayToDataTable([
    ['x', 'y'], 
    <?php echo json_encode(array($x, $y)) ?>
]);

Addition:

One way to combine the arrays would be to use array_map:

$x =  array(1,3,5);
$y = array(2,4,6);

$combined = array_map(function($v, $k) use ($y) {
  return array($v, $y[$k]);
}, $x, array_keys($x));

// prepend the array with headers
array_unshift($combined, array('x', 'y'));

echo json_encode( $combined );  // [["x","y"],[1,2],[3,4],[5,6]]

For PHP which is older than 5.3:

function combine_array_element($v, $k) {
    global $y;
    return array($v, $y[$k]);
}    
$combined = array_map(combine_array_element, $x, array_keys($x));
Sign up to request clarification or add additional context in comments.

5 Comments

i am new to javascript so i donot have much idea about it, could you please explain it in little detail or provide me link for documentation
JSON is a data format used to pass data around on the web. Since JSON is a subset of Javascript any valid JSON is valid javascript. To make it real simple - json_encode can take a php array and output javascript.
i wrote it like below in code but didn't work. var data = google.visualization.arrayToDataTable([ ['x', 'y'], [<?php echo json_encode(array($x,$y)) ?>], ]);
okay got the problem...its printing whole x array first n then whole y array. [["700","750","600"],["300","550","400"]] each array has three element in this case:::first is whole x array and second one is whole y array Can we print it like : [["700","300"], ["750","550"], ["600","400"] ] [first element of array x,first element of array y]
Sorry I guess I should have read though the reference more carefully. I edited my answer - should now give [["label x","label y"],[x1,y1],[x2,y2]...] as per the example on developers.google.com/chart/interactive/docs/#assortedmethods

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.