嗨,我是 PHP 新手,尝试使用 php sql 获取以下响应,但我无法找到这样的期望输出
[{"Id":1, "name": "India", "Cities":[{"Id":1, "Name":"Mumbai", "country_id":1}, {"Id":2,"Name":"Delhi","country_id":1},
"id":3,"Name":Banglore","country_id":1}, {"Id":2, "Name":"USA", "Cities":[{"Id":6, "Name":"New York", "country_id":2},.....
我有两张表,一张是基于国家/地区的,另一张是基于城市的。
I tried
<?php
include_once("config.inc.php");
$sql = "SELECT * FROM country";
$sqlCity = "SELECT * FROM city";
$cityQuery = mysqli_query($conn, $sqlCity);
$sqlQuery = mysqli_query($conn, $sql);
$mainArray = array();
if(mysqli_num_rows($cityQuery) > 0 ){
$cityResponse = array();
while($resCity = mysqli_fetch_assoc($cityQuery)){
$cityResponse[] = $resCity;
}
if(mysqli_num_rows($sqlQuery) > 0 ){
$response = array();
while($res = mysqli_fetch_assoc($sqlQuery)){
$response[] = $res;
}
foreach($cityResponse as $city){
foreach($response as $country){
if($city['country_id'] == $country['id']){
$mainArray = array("Cities" => $city);
}
}
}
echo '{"response": '.json_encode($response).', '.json_encode($mainArray).' "success": true}';
}
}else{
echo '{"response": '.json_encode($response).' "success": false}';
}
?>
目前我的回复显示
{"response": [{"id":"1","name":"India"},{"id":"2","name":"USA"},{"id":"3","name":"UK"}], {"Cities":{"id":"15","name":"Manchester","country_id":"3"}} "success": true}
最佳答案
有关详细代码说明,请查看内嵌注释
- 使用相关列名称修改 SQL 查询
- 你必须照顾的内存。默认情况下,5.3 中 php 内存限制为 128MB
检查代码并告诉我结果
<?php $data = array(); //include your database configuration files include_once("config.inc.php"); //execute the join query to fetch the result $sql = "SELECT country.country_id, country.name AS country_name,". " city.city_id, city.name AS city_name FROM country ". " JOIN city ON city.country_id=country.country_id ". " ORDER BY country.country_id "; //execute query $sqlQuery = mysqli_query($conn, $sql) or die('error exists on select query'); //check the number of rows count if(mysqli_num_rows($sqlQuery) > 0 ){ //country id temprory array $country_id = array(); //loop each result while($result = mysqli_fetch_assoc($sqlQuery)){ //check the country id is already exist the only push the city entries if(!in_array($result['country_id'],$country_id)) { //if the city is for new country then add it to the main container if(isset($entry) && !empty($entry)) { array_push($data, $entry); } //create entry array $entry = array(); $entry['Id'] = $result['country_id']; $entry['name'] = $result['country_name']; $entry['Cities'] = array(); //create cities array $city = array(); $city['Id'] = $result['city_id']; $city['name'] = $result['city_name']; $city['country_id'] = $result['country_id']; //append city entry array_push($entry['Cities'], $city); $country_id[] = $result['country_id']; } else { //create and append city entry only $city = array(); $city['Id'] = $result['city_id']; $city['name'] = $result['city_name']; $city['country_id'] = $result['country_id']; array_push($entry['Cities'], $city); } } } //display and check the expected results echo json_encode($data);
关于php - 从两个表中获取记录并转换为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41720478/