【问题标题】:Generating Custom JSON in PHP在 PHP 中生成自定义 JSON
【发布时间】:2017-02-09 12:46:25
【问题描述】:

我有一个需要在 PHP 中以 json 格式编码的数据库查询,我需要以特定格式对数据进行编码。

我想要的格式是

{
    "Car tyre Showroom":{"ABCD":"1234567","CDEF":"90000000","PPPP":"1000000"},
    "Bike tyre Showroom":{"AFGH":"32124235","AAAAA":"9111111111"},
    "Car Driving School":{"AMNB ":"45565778"},
    "Car battery shop":{"PLQA":"4235346456"}
}

但我得到的格式是

    {
        "Car tyre Showroom":{"PPPP":"1000000"}
    },{
        "Bike tyre Showroom":{"AAAAA":"9111111111"}
    },{
        "Car Driving School":{"AMNB ":"45565778"}
    },{
        "Car battery shop":{"PLQA":"4235346456"}
    }

我的数据库查询是:

 $query = "select S.SpecificCategoryName,A.* from specificcategories S,areaspecificdealers A where A.SpecificCategoryId=S.SpecificCategoryId and A.LocationCode=(Select LocationCode from arealist where LocationName='".$location."')"; 

我已经用这种方式编码了 json,

 for($col = 0; $col < count($result); $col++)
{




        $values[$result[$col]['SpecificCategoryName']]= array($result[$col]['ClientName']=>$result[$col]['PhoneNumber']);


}

回显 json_encode($values);

请帮助如何以上述方式对数据进行编码。

【问题讨论】:

    标签: php json


    【解决方案1】:

    这应该可以解决问题:

    $result=array();    
    for($row = 0; $row < count($result); $row++)
        {
               $result[$result[$row]['SpecificCategoryName']]= array($result[$row]['DealerName1']=>$result[$row]['PhoneNumber1']);
        }
    echo json_encode($result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 2018-05-02
      相关资源
      最近更新 更多