3 回答
TA贡献1836条经验 获得超5个赞
您正在寻找的函数是json_encode(),但如果您首先构建输入数组,它会更有用。
所以像下面这样:
// Some code here to generate $arrayData from $statements, giving the following
$arrayData = [
['Country' => 'China', 'Color Value' => 3],
['Country' => 'Russia', 'Color Value' => 2.6],
['Country' => 'France', 'Color Value' => 2.5],
['Country' => 'Spain', 'Color Value' => 2.4],
['Country' => 'Portugal', 'Color Value' => 1.1]
];
// Then, once you have generated $arrayData
echo json_encode($arrayData);
TA贡献1799条经验 获得超8个赞
您需要先在 php 中准备数组,而不仅仅是 echo json_encode($arr):
<?php
$data = [];
foreach ($something as $statements) {
$data[] = [$statements["Country"], $statements["Color_Value"]];
}
$arrayData = json_encode($data);
?>
arrayData = <?= $arrayData ?>;
TA贡献1836条经验 获得超4个赞
//First you need to check the structure of the result from your database query
//Then Loop through the result Object/Array to get the desired structure
//Most Common way of doing this is foreach loop
//define an blank array
$chart = [];
//put your axis tags/ texts in on first index
$chart[] = ['Country', 'Colour Count'];
//let's say $reult stores the data fetched from database
foreach ($result as $key => $value){
//let's assume $value has field with country name as value
//and field colour_count have color count values, so
// $value->country = Country Name
//$value->colour_count = Color Count Value
$chart[] = [$value->country, $value->colour_count];
}
// Once the Loop is finished, you will get an array with your desired format
//$chart = [['Country', 'Colour Count'],['France', 12],['USA',34],.....so on];
- 3 回答
- 0 关注
- 134 浏览
添加回答
举报