1

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!

1

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Does not work because $row['Value'] is string, I need number: 9 not "9". Thank you!

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.