构建JSON数据集 - PHP

时间:2015-03-23 12:43:30

标签: php json

我从我的表中获得了这样的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
}

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

尚未经过测试,请查看此内容。

   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
    }

答案 1 :(得分:0)

尝试这样,

$result[] = array();
$result['label'] = "OPE";
                while($row = $r->fetch_row()) {
    $row[0] = strtotime($row[0]);
    $result['data'] = $row;
}

答案 2 :(得分:0)

  

对$ 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
    }