[英]convert string into array for highcharts js
My code PHP generate a string variable 我的代码PHP生成一个字符串变量
<?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:' ? 在js中,如何将“名称”变量转换为“数据:”的功能变量?
<script>
var name = "<?php echo $result; ?>";
Highcharts.chart('container', {
...
data: [
['Shanghai', 23.7],
['Lagos', 16.1],
['Istanbul', 14.2]
];
...
});
</script>
Thank you! 谢谢!
You need to wrap $result
in an array: 您需要将$result
包装在数组中:
$result = "[$result]";
But you really should use json_encode
like this: 但是您确实应该像这样使用json_encode
:
$resultArr = [];
foreach ($rows as $row) {
$resultArr[] = [$row['Name'], $row['Value']];
}
$result = json_encode($resultArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.