2017-02-09 68 views
0

我有一个数据库查询需要在PHP中以json格式进行编码,我需要以特定格式对数据进行编码。在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']); 


} 

echo json_encode($ values);

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

回答

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);