Highcharts Mysql Json 数组 PHP
Posted
技术标签:
【中文标题】Highcharts Mysql Json 数组 PHP【英文标题】:Highcharts Mysql Json arrays PHP 【发布时间】:2018-03-23 01:45:00 【问题描述】:我正在尝试为我的 highchart 散点图获取正确的 Json 输出,请参阅 example。
我想要的Json输出:
[
"name": "Female",
"color": "red",
"data": [
"name": "Anna",
"x": 161.2,
"y": 51.6
,
"name": "Clair",
"x": 167.5,
"y": 59.0
]
,
"name": "Male",
"color": "blue",
"data": [
"name": "James",
"x": 174.0,
"y": 65.6
]
,
"name": "Peet",
"color": "black",
"data": [
"name": "Peet",
"x": 175.3,
"y": 71.8
]
]
数据.php
<?php
$arr = array();
$arr1 = array();
$arr2 = array();
$result = array();
$sql = "SELECT `gender`, `name`, `length`, `weight`
FROM `highcharts_data`
WHERE `gender` = 'Female'";
$q = mysqli_query($mysqli,$sql);
while($row = mysqli_fetch_assoc($q))
$arr['name'] = 'Female';
$arr['data'] = array([(float)$row['length'], (float)$row['weight']]);
$arr['color'] = 'red';
array_push($result,$arr);
$sql1 = "SELECT `gender`, `name`, `length`, `weight`
FROM `highcharts_data`
WHERE `gender` = 'Male' AND `name` != 'Peet'";
$q1 = mysqli_query($mysqli,$sql1);
while($row = mysqli_fetch_assoc($q1))
$arr1['name'] = 'Male';
$arr1['data'] = array([(float)$row['length'], (float)$row['weight']]);
$arr1['color'] = 'blue';
array_push($result,$arr1);
$sql2 = "SELECT `gender`, `name`, `length`, `weight`
FROM `highcharts_data`
WHERE `gender` = 'Male' AND `name` = 'Peet'";
$q2 = mysqli_query($mysqli,$sql2);
while($row = mysqli_fetch_assoc($q2))
$arr2['name'] = 'Peet';
$arr2['data'] = array([(float)$row['length'], (float)$row['weight']]);
$arr2['color'] = 'black';
array_push($result,$arr2);
print json_encode($result, JSON_NUMERIC_CHECK);
mysqli_close($mysqli);
?>
我得到的 JSON 输出:
[
"name": "Female",
"data": [
[161.2, 51.6]
],
"color": "red"
,
"name": "Female",
"data": [
[167.5, 59]
],
"color": "red"
,
"name": "Male",
"data": [
[174, 65.6]
],
"color": "blue"
,
"name": "Peet",
"data": [
[175.3, 71.8]
],
"color": "black"
]
谁能告诉我如何更改 data.php 文件,以便我得到正确的 Json 输出!
谢谢!
【问题讨论】:
数据:[name: 'Anna',x: 161.2,y: 51.6,你在哪里得到这个名字? 【参考方案1】:这是一个如何将 PHP 格式化为 JSON 的工作示例。这不包括 mysql 部分,但这应该不很难弄清楚。
2017 年 10 月 12 日更新,更正了双重数据的错误:
2017 年 10 月 16 日更新,更正了没有数组推送的错误:
<?php
$males = array();
$malepersons = array();
$females = array();
$femalepersons = array();
$result = array();
$values = [[
"name" => 'Anna',
"height" => 175,
"weight" => 53.4,
"gender" => 'Female'
],[
"name" => 'Dan',
"height" => 185.1,
"weight" => 90.4,
"gender" => 'Male'
] ];
//Simplified for loop
foreach ($values as $row)
$person = array();
$person['name'] = $row['name'];
$person['x'] = $row['height'];
$person['y'] = $row['weight'];
if($row['gender'] == 'Male')
array_push($malepersons, $person);
elseif($row['gender'] == 'Female')
array_push($femalepersons, $person);
//took these out of the for loop, only need to be set once
$males['color'] = 'black';
$males['name'] = 'Male';
$males['data'] = $malepersons;
$females['color'] = 'green';
$females['name'] = 'Female';
$females['data'] = $femalepersons;
array_push($result, $males);
array_push($result, $females);
print json_encode($result, JSON_NUMERIC_CHECK);
?>
这将为您提供如下所示的 JSON:
[
"color": "black",
"name": "Male",
"data": [
"name": "Dan",
"x": 185.1,
"y": 90.4
]
,
"color": "green",
"name": "Female",
"data": [
"name": "Anna",
"x": 175,
"y": 53.4
]
]
highcharts 希望作为系列输入。
在您的情况下,$values
数组实际上是 SQL 返回的数据,您只需将循环更改为循环您想要循环的内容。
添加了工作示例:https://ideone.com/RPp3DO
键控数组上的 PHP DOCS:http://php.net/manual/en/language.types.array.php
【讨论】:
您好 Ewolden,感谢您的快速回复!这和我的想法很接近。在您的 Json 输出中,“数据”被提及两次。这在示例文件中不起作用。我是 php 和 Json 领域的绝对初学者。我已经开始使用以下 data.php 文件(下一篇文章),但这是我目前得到的输出:["color":"black","name":"Male","data":"name":"Peet","data":"x":175.3,"y":71.8,"color":"green","name":"Female","data":"name":"Clair","data":"x":167.5,"y":59]
.
Part1: <?php $result = array(); $sql = "SELECT
gender,
name,
length,
weight` FROM highcharts_data
"; $q = mysqli_query($mysqli,$sql); while ($row = mysqli_fetch_assoc($q)) $person = array(); if($row['gender'] == 'Male') $person['name'] = $row['name']; $person['数据'] = ['x' => $row['length'], 'y' => $row['weight']]; $males['color'] = 'black'; $males['name' ] = '男性'; $males['data'] = $person; `
elseif($row['gender'] == 'Female') $person['name'] = $row['name']; $person['data'] = ['x' => $row['length'], 'y' => $row['weight']]; $females['color'] = '绿色'; $females['name'] = '女'; $females['data'] = $person; array_push($result, $males); array_push($result, $females);打印 json_encode($result, JSON_NUMERIC_CHECK); mysqli_close($mysqli); ?>
再次感谢!输出以正确的方式显示,但缺少部分。目前我有两个组,即女性和男性。我想念第三组“Peet”。我想让这个人与其他群体分开。你能在你的例子中添加这个吗?我还将添加一个新答案,其中包含我迄今为止所做的更改。
对不起,我错过了一部分。循环不起作用。我只得到一个人的数据,而我期望两个人的数据,就像我的例子一样。能不能也看看这部分。以上是关于Highcharts Mysql Json 数组 PHP的主要内容,如果未能解决你的问题,请参考以下文章
使用 PHP/MySQL 的 highcharts 的 Json 结构
来自 php mysql 的 Highcharts 钻取 json
使用 JSON 将数据从 PHP 数组传递到 Highcharts 图表
highcharts中用<%=%>获得一个数组,如何将该数组赋给data呀?????