从PHP SQL循环生成JSON数组

时间:2017-04-22 11:08:24

标签: php json

我需要从PHP循环和SQL DB生成以下JSON,但是我遇到了麻烦:

    [
      {
        "ItemName": "Websites1",
        "Websites1": [
          {
            "0": "1",
            "ID": "1",
            "1": "Essential",
            "WebsiteName": "Essential 1",
            "2": "EI",
            "WebsiteCode": "EI"
          },
          {
            "0": "2",
            "ID": "2",
            "1": "Icon Ibiza",
            "WebsiteName": "Icon 1",
            "2": "IC",
            "WebsiteCode": "IC"
          },
          {
            "0": "3",
            "ID": "3",
            "1": "So Ibiza",
            "WebsiteName": "So 1",
            "2": "SO",
            "WebsiteCode": "SO"
          }
        ]
      },
      {
        "ItemName": "Websites2",
        "Websites2": [
          {
            "0": "1",
            "ID": "1",
            "1": "Essential Ibiza",
            "WebsiteName": "Essential 2",
            "2": "EI",
            "WebsiteCode": "EI"
          },
          {
            "0": "2",
            "ID": "2",
            "1": "Icon Ibiza",
            "WebsiteName": "Icon 2",
            "2": "IC",
            "WebsiteCode": "IC"
          },
          {
            "0": "3",
            "ID": "3",
            "1": "So Ibiza",
            "WebsiteName": "So 2",
            "2": "SO",
            "WebsiteCode": "SO"
          }
        ]
      }
    ]

我将相关数据从DB返回到PHP中,但我无法确定如何使用PHP循环生成相关的键值对和嵌套。我到目前为止的代码是:

            $this->db->sql =    "SELECT ID AS ID, WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";

            $rs = $this->db->query($this->db->sql);

            if ($this->db->row_count != 0) {

                $response = array();
                $json = array();

                while ($row = $this->db->row_read($rs)) {

                    $json["ID"] = $row["ID"];
                    $json["SelectText"] = $row["SelectText"];
                    $json["SelectValue"] = $row["SelectValue"];

                    //$json[] = $row;                               
                }

                $response["Websites1"] = $json;


            }


            $rs = $this->db->query($this->db->sql);

            if ($this->db->row_count != 0) {

                $json2 = array();

                while ($row = $this->db->row_read($rs)) {

                    $json2["ID"] = $row["ID"];
                    $json2["SelectText"] = $row["SelectText"];
                    $json2["SelectValue"] = $row["SelectValue"];

                    //$json[] = $row;                               
                }

                $response["Websites2"] = $json2;

            }



            return '[' . json_encode($response) . ']';

我显然做了一些非常错误的事情,因为每个查询最终只有一行。每个循环都会覆盖键值对。嵌套也不正确。对于这样一个愚蠢的问题我感到很抱歉,但我一直试图通过网上信息解决这个问题并且卡住了。

谢谢,

中午。

编辑 - 管理使用下面的Anushil Nandan提供的答案进行修复,但PHP需要进行一些额外的调整才能获得所需的格式:

            // -----------------------------------------------------------------------------------------------
            // BANNERMANGEMENTFORM
            function bannerManagementJson($JsonItem) {

                $this->db->connect();

                $response = array();

                //Generate Websites drop-down
                $SqlStr = "SELECT WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";
                $response[] = $this->retrieveFormElementJson($SqlStr,'Websites',1);

                //Generate Clients drop-down
                $SqlStr = "SELECT ClientName AS SelectText, ID AS SelectValue FROM Banners_BannerClients ORDER BY ClientName";
                $response[] = $this->retrieveFormElementJson($SqlStr,'Clients',2);

                return json_encode($response);

            }   

            // -----------------------------------------------------------------------------------------------
            // RETRIEVEFORMELEMENTJSON
            function retrieveFormElementJson($SqlStr,$ElementName,$SelectListNo) {

                $this->db->sql = $SqlStr;

                $rs = $this->db->query($this->db->sql);

                if ($this->db->row_count != 0) {

                    $json = array();

                    while ($row = $this->db->row_read($rs)) {
                        $json[] = $row;
                    }

                    $arrayResponse = array("ItemName"=>$ElementName, "SelectList" . $SelectListNo=>$json);

                }

                return $arrayResponse;

            }   

1 个答案:

答案 0 :(得分:1)

每次进入while循环

时,您的数组都会被覆盖

尝试: $json1[] = array(); $json2[] = array();

        $this->db->sql =    "SELECT ID AS ID, WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";

        $rs = $this->db->query($this->db->sql);

        if ($this->db->row_count != 0) {

            $response = array();
            $json[] = array();

            while ($row = $this->db->row_read($rs)) {

                $json[]["ID"] = $row["ID"];
                $json[]["SelectText"] = $row["SelectText"];
                $json[]["SelectValue"] = $row["SelectValue"];

                //$json[] = $row;                               
            }

            $response["Websites1"] = $json;


        }


        $rs = $this->db->query($this->db->sql);

        if ($this->db->row_count != 0) {

            $json2[] = array();

            while ($row = $this->db->row_read($rs)) {

                $json2[]["ID"] = $row["ID"];
                $json2[]["SelectText"] = $row["SelectText"];
                $json2[]["SelectValue"] = $row["SelectValue"];

                //$json[] = $row;                               
            }

            $response["Websites2"] = $json2;

        }



        return '[' . json_encode($response) . ']';