找不到数据时如何发送空白数组?



我在PHP中开发了State->city->区域明智的选择。 一切正常。但是在它删除city数组city中找不到任何数据。 我想将空city array传递给 API 响应 当没有从数据库中找到数据时。我该怎么做?

我的代码如下所示

if (isset($credentials->credentials->uid) && $credentials->credentials->uid != ""):
$mainArr = array();
$stateArr = array();
$state = $this->Data_model->Custome_query('SELECT mf_state_id, name, status FROM mf_state WHERE status = "A"');
foreach ($state as $s) {
$sArr = array();
$sArr = array(
'mf_state_id' => $s['mf_state_id'],
'name' => $s['name'],
'status' => $s['status']
);
$cityArr = array();
$city = $this->Data_model->Custome_query('SELECT mf_city_id, mf_state_id, name, status, active FROM mf_city WHERE status = "A" AND active = "S" AND mf_state_id = '.$s['mf_state_id']);
foreach ($city as $c) {
$cArr = array();
$cArr = array(
'mf_city_id' => $c['mf_city_id'],
'mf_state_id' => $c['mf_state_id'],
'name' => $c['name'],
'status' => $c['status'],
'active' => $c['active']
);
$areaArr = array();
$area = $this->Data_model->Custome_query('SELECT mf_area_id, mf_city_id, area, status FROM mf_area WHERE status = "A" AND mf_city_id = '.$c['mf_city_id']);
foreach ($area as $a) {
$aArr = array();
$aArr = array(
'mf_area_id' => $a['mf_area_id'],
'mf_city_id' => $a['mf_city_id'],
'area' => $a['area'],
'status' => $a['status'],
);

$areaArr[] = $aArr;
$cArr['area'] = $areaArr;
}

// print_r($cityArr);
$cityArr[] = $cArr;
$sArr['cities'] = $cityArr;
// print_r($sArr['cities']);
}
$stateArr[] = $sArr;
}
// print_r($sArr['cities']);
$mainArr = $stateArr;

echo str_replace("/", "/", json_encode( ['res' => 0, 'msg' => 'Successfully got City Area data', 'data' => $mainArr, 'version' => $this->version,"qry"=>""]));

我得到的回应看起来像这样

{
"res": 0,
"msg": "Successfully got City Area data",
"data": [
{
"mf_state_id": "1",
"name": "Gujarat",
"status": "A",
"cities": [
{
"mf_city_id": "1",
"mf_state_id": "1",
"name": "Surat",
"status": "A",
"active": "S",
"area": [
{
"mf_area_id": "1",
"mf_city_id": "1",
"area": "Adajan",
"status": "A"
},
{
"mf_area_id": "2",
"mf_city_id": "1",
"area": "Bhatar",
"status": "A"
},
{
"mf_area_id": "3",
"mf_city_id": "1",
"area": "Dindoli",
"status": "A"
},
{
"mf_area_id": "4",
"mf_city_id": "1",
"area": "Kapodra",
"status": "A"
},
{
"mf_area_id": "5",
"mf_city_id": "1",
"area": "Mota Varachha",
"status": "A"
},
{
"mf_area_id": "6",
"mf_city_id": "1",
"area": "Nana Varachha",
"status": "A"
},
{
"mf_area_id": "7",
"mf_city_id": "1",
"area": "Palanpur Patiya",
"status": "A"
},
{
"mf_area_id": "8",
"mf_city_id": "1",
"area": "Parvat Patiya",
"status": "A"
},
{
"mf_area_id": "9",
"mf_city_id": "1",
"area": "Udhna",
"status": "A"
},
{
"mf_area_id": "10",
"mf_city_id": "1",
"area": "Athwa",
"status": "A"
},
{
"mf_area_id": "11",
"mf_city_id": "1",
"area": "Bhatha",
"status": "A"
},
{
"mf_area_id": "12",
"mf_city_id": "1",
"area": "Ghoddod Road",
"status": "A"
},
{
"mf_area_id": "13",
"mf_city_id": "1",
"area": "Katargam",
"status": "A"
},
{
"mf_area_id": "14",
"mf_city_id": "1",
"area": "Pandesara",
"status": "A"
},
{
"mf_area_id": "15",
"mf_city_id": "1",
"area": "Piplod",
"status": "A"
},
{
"mf_area_id": "16",
"mf_city_id": "1",
"area": "Vesu",
"status": "A"
},
{
"mf_area_id": "17",
"mf_city_id": "1",
"area": "Citylight",
"status": "A"
},
{
"mf_area_id": "18",
"mf_city_id": "1",
"area": "Jahangirpura",
"status": "A"
},
{
"mf_area_id": "19",
"mf_city_id": "1",
"area": "Pal",
"status": "A"
},
{
"mf_area_id": "20",
"mf_city_id": "1",
"area": "Parle Point",
"status": "A"
},
{
"mf_area_id": "21",
"mf_city_id": "1",
"area": "Rander",
"status": "A"
},
{
"mf_area_id": "28",
"mf_city_id": "1",
"area": "Zampa Bazaar",
"status": "A"
},
{
"mf_area_id": "40",
"mf_city_id": "1",
"area": "Rander Road",
"status": "A"
},
{
"mf_area_id": "43",
"mf_city_id": "1",
"area": "Varachha Road",
"status": "A"
},
{
"mf_area_id": "58",
"mf_city_id": "1",
"area": "Palanpur Gam",
"status": "A"
},
{
"mf_area_id": "63",
"mf_city_id": "1",
"area": "Palanpur Jakatnaka",
"status": "A"
},
{
"mf_area_id": "92",
"mf_city_id": "1",
"area": "New Causeway Road",
"status": "A"
}
]
}
]
},
{
"mf_state_id": "2",
"name": "Maharastra",
"status": "A"
}
],
"version": "1.0",
"qry": ""
}

在最近stateMaharastra数据库中没有城市。 但它应该返回city数组空白。

我希望响应看起来像这样。找不到数据时,城市数组应为空数组


{
"res": 0,
"msg": "Successfully got City Area data",
"data": [
{
"mf_state_id": "1",
"name": "Gujarat",
"status": "A",
"cities": [
{
"mf_city_id": "1",
"mf_state_id": "1",
"name": "Surat",
"status": "A",
"active": "S",
"area": [
{
"mf_area_id": "1",
"mf_city_id": "1",
"area": "Adajan",
"status": "A"
},
{
"mf_area_id": "2",
"mf_city_id": "1",
"area": "Bhatar",
"status": "A"
},
{
"mf_area_id": "3",
"mf_city_id": "1",
"area": "Dindoli",
"status": "A"
},
{
"mf_area_id": "4",
"mf_city_id": "1",
"area": "Kapodra",
"status": "A"
},
{
"mf_area_id": "5",
"mf_city_id": "1",
"area": "Mota Varachha",
"status": "A"
},
{
"mf_area_id": "6",
"mf_city_id": "1",
"area": "Nana Varachha",
"status": "A"
},
{
"mf_area_id": "7",
"mf_city_id": "1",
"area": "Palanpur Patiya",
"status": "A"
},
{
"mf_area_id": "8",
"mf_city_id": "1",
"area": "Parvat Patiya",
"status": "A"
},
{
"mf_area_id": "9",
"mf_city_id": "1",
"area": "Udhna",
"status": "A"
},
{
"mf_area_id": "10",
"mf_city_id": "1",
"area": "Athwa",
"status": "A"
},
{
"mf_area_id": "11",
"mf_city_id": "1",
"area": "Bhatha",
"status": "A"
},
{
"mf_area_id": "12",
"mf_city_id": "1",
"area": "Ghoddod Road",
"status": "A"
},
{
"mf_area_id": "13",
"mf_city_id": "1",
"area": "Katargam",
"status": "A"
},
{
"mf_area_id": "14",
"mf_city_id": "1",
"area": "Pandesara",
"status": "A"
},
{
"mf_area_id": "15",
"mf_city_id": "1",
"area": "Piplod",
"status": "A"
},
{
"mf_area_id": "16",
"mf_city_id": "1",
"area": "Vesu",
"status": "A"
},
{
"mf_area_id": "17",
"mf_city_id": "1",
"area": "Citylight",
"status": "A"
},
{
"mf_area_id": "18",
"mf_city_id": "1",
"area": "Jahangirpura",
"status": "A"
},
{
"mf_area_id": "19",
"mf_city_id": "1",
"area": "Pal",
"status": "A"
},
{
"mf_area_id": "20",
"mf_city_id": "1",
"area": "Parle Point",
"status": "A"
},
{
"mf_area_id": "21",
"mf_city_id": "1",
"area": "Rander",
"status": "A"
},
{
"mf_area_id": "28",
"mf_city_id": "1",
"area": "Zampa Bazaar",
"status": "A"
},
{
"mf_area_id": "40",
"mf_city_id": "1",
"area": "Rander Road",
"status": "A"
},
{
"mf_area_id": "43",
"mf_city_id": "1",
"area": "Varachha Road",
"status": "A"
},
{
"mf_area_id": "58",
"mf_city_id": "1",
"area": "Palanpur Gam",
"status": "A"
},
{
"mf_area_id": "63",
"mf_city_id": "1",
"area": "Palanpur Jakatnaka",
"status": "A"
},
{
"mf_area_id": "92",
"mf_city_id": "1",
"area": "New Causeway Road",
"status": "A"
}
]
}
]
},
{
"mf_state_id": "2",
"name": "Maharastra",
"status": "A",
"cities": []
}
],
"version": "1.0",
"qry": ""
}

您应该使用 cities 键初始化$sArr为空数组。如果随后找到该州的城市,则cities键将在循环中被覆盖,否则您将获得空值。

$sArr = array();
$sArr = array(
'mf_state_id' => $s['mf_state_id'],
'name' => $s['name'],
'status' => $s['status'],
'cities' => array(),    //Initialize with empty array here.
);

试试这个,如果州没有城市或为空,则其他代码不会执行。

if(count($city)>0){
foreach ($city as $c) {
$cArr = array(
'mf_city_id' => $c['mf_city_id'],
'mf_state_id' => $c['mf_state_id'],
'name' => $c['name'],
'status' => $c['status'],
'active' => $c['active']
);
}
}else{
$cArr = array();
}

相关内容

  • 没有找到相关文章

最新更新