使用php将mysql数据转换为json以及其他信息

7kqas0il  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(306)

代码

<?php
include "dbconfig.php";
include "yrsetup.php";
$tdate='2018-11-19';
$tmarket='sirsi';
$sql="SELECT ALLCODES.NAME AS Type,MIN(LOT1819.RATE) AS 
Min,MAX(LOT1819.RATE) AS Max,ROUND(SUM(LOT1819.RATE)/COUNT(LOTNO),0) AS avg 
FROM LOT1819 
    LEFT JOIN ALLCODES  ON allcodes.`sw`='IT' AND allcodes.`cd`=APCD
    WHERE MRK IS NULL AND DATE1='2018-11-19' AND LOT1819.SW IS NULL GROUP BY 
APCD";    

        $result=mysqli_query($conn,$sql);
        $response = array();
        $response = array('Date' => $tdate, 'market' => $tmarket);
        while($row=mysqli_fetch_assoc($result))
            {
                array_push($response,$row);
            }
            $outsidebrace=')';
            array_push($response,$outsidebrace);
            echo json_encode($response);
            //echo json_encode($response);
?>

结果
{“date”:“2018-11-19”,“market”:“sirsi”,“0”:{“type”:“rashi”,“min”:“30358”,“max”:“32699”,“avg”:“31505”},“1”:{“type”:“bette”,“min”:“21099”,“max”:“29618”,“avg”:“27110”},“2”:{“type”:“muri”,“min”:“23121”,“max”:“23809”,“avg”:“23465”},“3”:{“type”:“kole”,“min”:“18019”,“max”:“23558”,“avg”:“21928”},“4”:{“type”:“k.g.”,“min”:“14199”,“max”:“22609”,“avg”:“19109”},“5”:{“type”:“chali”,“min”:“24039”,“max”:“26488”,“avg”:“25773”},“6”:{“type”:“b.g.”,“min”:“16699”,“max”:“22300”,“avg”:“21109”},“7”:{“type”:“11 chali”,“min”:“22720”,“max”:“23899”,“avg”:“23512”},“8”:{“type”:“koka”,“min”:“11589”,“max”:“20699”,“avg”:“16902”},“9”:{“type”:“ch.kemp”,“min”:“18315”,“max”:“24079”,“avg”:“21862”},“10”:{“type”:“aragere”,“min”:“22809”,“max”:“22809”,“avg”:“22809”},“11”:{“type”:“ke.bette”,“min”:“23731”,“max”:“24631”,“avg”:“24128”},“12”:{“type”:“pepper”,“min”:“34699”,“max”:“34899”,“avg”:“34799”},“13”:{“type”:“chakra”,“min”:“17869”,“max”:“23119”,“avg”:“20494”},“14”:{“type”:“bt rashi”,“min”:“26899”,“max”:“29718”,“avg”:“28431”},“15”:{“type”:“muri bette”,“min”:“21490”,“max”:“21699”,“avg”:“21594”},“16”:{“type”:“muri rashi”,“min”:“25699”,“max”:“25699”,“avg”:“25699”},“17”:{“type”:“churu”,“min”:“7299”,“max”:“18009”,“avg”:“12654”},“18”:{“type”:“la koka”,“min”:“6199”,“max”:“9399”,“avg”:“7799”},“19”:{“type”:“la b got”,“min”:“11899”,“max”:“18499”,“avg”:“15666”},“20”:“type”:“dbl chali”,“min”:“26418”,“max”:“26418”,“avg”:“26418”},“21”:{“type”:“got bette”,“min”:“14099”,“max”:“22421”,“avg”:“18260”},“22”:{“type”:“s bette”,“min”:“29518”,“max”:“29518”,“avg”:“29518”},“23”:“}”
这是有效的json,但我需要以下格式的结果

{
    "Date": "2018-11-19",
    "market": "Sirsi",
    "data": [{
        "Type": "RASHI",
        "Min": "30358",
        "Max": "32699",
        "avg": "31505"
    }, {
        "Type": "BETTE",
        "Min": "21099",
        "Max": "29618",
        "avg": "27110"
    }, {
        "Type": "MURI",
        "Min": "23121",
        "Max": "23809",
        "avg": "23465"
    }, {
        "Type": "KOLE",
        "Min": "18019",
        "Max": "23558",
        "avg": "21928"
    }, {
        "Type": "K.G.",
        "Min": "14199",
        "Max": "22609",
        "avg": "19109"
    }, {
        "Type": "CHALI",
        "Min": "24039",
        "Max": "26488",
        "avg": "25773"
    }, {
        "Type": "B.G.",
        "Min": "16699",
        "Max": "22300",
        "avg": "21109"
    }, {
        "Type": "11-CHALI",
        "Min": "22720",
        "Max": "23899",
        "avg": "23512"
    }, {
        "Type": "KOKA",
        "Min": "11589",
        "Max": "20699",
        "avg": "16902"
    }, {
        "Type": "CH.KEMP",
        "Min": "18315",
        "Max": "24079",
        "avg": "21862"
    }, {
        "Type": "ARAGERE",
        "Min": "22809",
        "Max": "22809",
        "avg": "22809"
    }, {
        "Type": "KE.BETTE",
        "Min": "23731",
        "Max": "24631",
        "avg": "24128"
    }, {
        "Type": "PEPPER",
        "Min": "34699",
        "Max": "34899",
        "avg": "34799"
    }, {
        "Type": "CHAKRA",
        "Min": "17869",
        "Max": "23119",
        "avg": "20494"
    }, {
        "Type": "BT RASHI",
        "Min": "26899",
        "Max": "29718",
        "avg": "28431"
    }, {
        "Type": "MURI BETTE",
        "Min": "21490",
        "Max": "21699",
        "avg": "21594"
    }, {
        "Type": "MURI RASHI",
        "Min": "25699",
        "Max": "25699",
        "avg": "25699"
    }, {
        "Type": "CHURU",
        "Min": "7299",
        "Max": "18009",
        "avg": "12654"
    }, {
        "Type": "LA KOKA",
        "Min": "6199",
        "Max": "9399",
        "avg": "7799"
    }, {
        "Type": "LA B GOT",
        "Min": "11899",
        "Max": "18499",
        "avg": "15666"
    }, {
        "Type": "DBL CHALI",
        "Min": "26418",
        "Max": "26418",
        "avg": "26418"
    }, {
        "Type": "GOT BETTE",
        "Min": "14099",
        "Max": "22421",
        "avg": "18260"
    }, {
        "Type": "S BETTE",
        "Min": "29518",
        "Max": "29518",
        "avg": "29518"
    }]
}

我怎样才能解决这个问题

ecfsfe2w

ecfsfe2w1#

像这样试试。您需要在主数组中创建“data”属性,然后将个别项添加到该属性中,而不是将它们添加到主数组中。我还删除了几行似乎是多余的。

$result=mysqli_query($conn,$sql);
$response = $response = array('Date' => $tdate, 'market' => $tmarket, "data"=> array());

while($row=mysqli_fetch_assoc($result))
{
    array_push($response["data"],$row);
}
echo json_encode($response);
1hdlvixo

1hdlvixo2#

你需要为数据建立一个嵌套数组,在你将它添加到顶层的那一刻。。。

$result=mysqli_query($conn,$sql);
$response = array('Date' => $tdate, 'market' => $tmarket
               , 'data' => array());
while($row=mysqli_fetch_assoc($result))
    {
        $response['data'][] = $row;
    }
echo json_encode($response);

所以在设置初始 $reponse 数组,我添加一个 data 具有空数组的元素。然后在循环中,我将新行添加到此项中(使用 [] 而不是 array_push() ).
我还删除了 $outsidebrace 一点都不需要。

相关问题