3 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
您正在尋找的函數(shù)是json_encode(),但如果您首先構(gòu)建輸入數(shù)組,它會(huì)更有用。
所以像下面這樣:
// 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貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要先在 php 中準(zhǔn)備數(shù)組,而不僅僅是 echo json_encode($arr):
<?php
$data = [];
foreach ($something as $statements) {
$data[] = [$statements["Country"], $statements["Color_Value"]];
}
$arrayData = json_encode($data);
?>
arrayData = <?= $arrayData ?>;

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
//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 關(guān)注
- 187 瀏覽
添加回答
舉報(bào)