[英]Structuring a JSON Data set - PHP
我從表中得到了這樣的JSON輸出。 此數據中的第一個數據是日期,第二個數據是11_OIC
[
[
984639600,
"23.49166667"
],
[
1521097200,
"22.985"
],
[
1552633200,
"22.34416667"
],
[
1584255600,
"19.98"
]
]
但是我正在尋找這樣的東西。
{
"label": "OPE",
"data": [
[
984639600,
"23.49166667"
],
[
1521097200,
"22.985"
],
[
1552633200,
"22.34416667"
],
[
1584255600,
"19.98"
]
]
}
我使用的PHP代碼如下:
private function productionhourlys(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
$query="SELECT distinct c.Date, c.11_OIC FROM productionhourlys c order by c.productionhourlyNumber desc";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
if($r->num_rows > 0){
$result[] = array();
while($row = $r->fetch_row()) {
$row[0] = strtotime($row[0]);
$result[] = $row;
}
$this->response($this->json($result), 200); // send user details
}
$this->response('',204); // If no records "No Content" status
}
謝謝您的幫助
還沒有測試,請檢查一下。
private function productionhourlys(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
$query="SELECT distinct c.Date, c.11_OIC FROM productionhourlys c order by c.productionhourlyNumber desc";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
if($r->num_rows > 0){
$result[] = array();
while($row = $r->fetch_row()) {
$row[0] = strtotime($row[0]);
$result[] = $row;
}
$pass = array(
'label' => 'OPE',
'data' => $result
);
$this->response($this->json($pass), 200); // send user details
}
$this->response('',204); // If no records "No Content" status
}
這樣嘗試
$result[] = array();
$result['label'] = "OPE";
while($row = $r->fetch_row()) {
$row[0] = strtotime($row[0]);
$result['data'] = $row;
}
在while中對$ result變量進行以下更改應進行必要的更改,以獲取所需的json格式。
private function productionhourlys(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
$query="SELECT distinct c.Date, c.11_OIC FROM productionhourlys c order by c.productionhourlyNumber desc";
$r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
if($r->num_rows > 0){
$result = array('label'=>'OPE');
$result['data'] = new array();
while($row = $r->fetch_row()) {
$row[0] = strtotime($row[0]);
$result[] = $row;
$result['data'][] = array(row[0],row[1]);
}
$this->response($this->json($result), 200); // send user details
}
$this->response('',204); // If no records "No Content" status
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.