Hexman
Hexman

Reputation: 327

convert string into array for highcharts js

My code PHP generate a string variable

<?php
foreach($rows as $row){

    $result .= "['" .$row["Name"]."'," .$row['Value']. "] , ";
}

$result = rtrim($result, ' , ');

//echo $result returns: ['FirstName1 LastName1',10] , ['FirstName2 LastName2',12] , ['FirstName3 LastName3 ',40]

?>

In js how can I convert the 'name' variable into a functional variable for 'data:' ?

<script>
var name = "<?php echo $result; ?>";

Highcharts.chart('container', {
...

data: [
    ['Shanghai', 23.7],
    ['Lagos', 16.1],
    ['Istanbul', 14.2]
  ];

...
});
</script>

Thank you!

Upvotes: 1

Views: 94

Answers (1)

Ethan
Ethan

Reputation: 4375

You need to wrap $result in an array:

$result = "[$result]";

But you really should use json_encode like this:

$resultArr = [];
foreach ($rows as $row) {
    $resultArr[] = [$row['Name'], $row['Value']];
}
$result = json_encode($resultArr);

Upvotes: 2

Related Questions