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 關注
- 171 瀏覽
添加回答
舉報