I developed State->city->area wise selection in PHP.
All things works fine. But there is no data found in city there it removed city array.
I want to pass Empty city array to API response When no data found from database. How can I do this ?
My code look like this
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"=>""]));
and response I am getting is look like this
{
"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": ""
}
In last state Maharastra there is no city in data base.
but it should return city array blank.
I want response look like this. City array should come null array when Data not found
{
"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": ""
}
You should initialize the $sArr with cities key as empty array. If the cities are then found of the state, the cities key will be overwritten in the loop else you will get empty values.
$sArr = array();
$sArr = array(
'mf_state_id' => $s['mf_state_id'],
'name' => $s['name'],
'status' => $s['status'],
'cities' => array(), //Initialize with empty array here.
);
Try this, If state have no cities or empty then other code does not execute.
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();
}
I have a JSON array like this
$countries = [
{
"id": "1",
"country_name": "Afghanistan",
},
{
"id": "2",
"country_name": "Albania",
},
{
"id": "3",
"country_name": "Algeria",
},
{
"id": "4",
"country_name": "American Samoa",
}
..
..
..
{
"id": "50",
"country_name": "Zimbabwe",
}
];
The following array contains, the list of countries that I need to sort to top
$top_countries = ['United States', 'United Kingdom', 'German'];
What is the best way to sort the above array as follows
$countries = [
{
"id": "30",
"country_name": "United States",
},
{
"id": "31",
"country_name": "United Kingdom",
},
{
"id": "20",
"country_name": "German",
},
{
"id": "1",
"country_name": "Afghanistan",
},
{
"id": "2",
"country_name": "Albania",
},
{
"id": "3",
"country_name": "Algeria",
},
{
"id": "4",
"country_name": "American Samoa",
}
..
..
..
{
"id": "50",
"country_name": "Zimbabwe",
}
];
// index "value" of country by name
$top = array_flip($top_countries);
usort($countries, function($a, $b) use ($top) {
// get "value" for each country, if it is in index
$aValue = isset($top[$a->country_name])?$top[$a->country_name]:-1;
$bValue = isset($top[$b->country_name])?$top[$b->country_name]:-1;
if ($aValue == $bValue) {
// preserve "original order", assuming they were ordered by id
return $a->id < $b->id ? -1 : 1;
}
return $aValue < $bValue ? 1:-1;
});
I am newbie to php ,trying to create a nested json array in php but not getting the desired json format.
desired format
{
"offers": [
{
"offerId": "1",
"offerName": "shirts",
"price": "1980",
"storeName": "peter england"
},
{
"offerId": "2",
"offerName": "shirts",
"storeId": "3",
"price": "2970",
"storeName": "peter england"
}
],
"brands": [
{
"brandName": "peter england",
"brandId": "5"
},
{
"brandName": "ruggers",
"brandId": "4"
}
],
"stores": [
{
"storeName": "peter england",
"storeId": "3"
},
{
"storeName": "peter england",
"storeId": "4"
}
],
"filters": [
{
"0": {
"filter": "M",
"filterId": "11"
},
"1": {
"filter": "S",
"filterId": "12"
},
"filterName": "size"
},
{
"0": {
"filter": "red",
"filterId": "13"
},
"1": {
"filter": "blue",
"filterId": "14"
},
"filterName": "colour"
}
]
}
but getting this format
{
"offers": [
{
"offerId": "1",
"offerName": "shirts",
"image": "http://www.offersmashup.com/testing/store/upload/sml_p1.jpg",
"brandId": "5",
"storeId": "3",
"price": "1980",
"offer": "Off-20",
"storeName": "peter england"
},
{
"offerId": "2",
"offerName": "shirts",
"image": "http://www.offersmashup.com/testing/store/upload/sml_p2.jpg",
"brandId": "5",
"storeId": "3",
"price": "2970",
"offer": "Off-30",
"storeName": "peter england"
}
],
"brands": [
{
"brandName": "peter england",
"brandId": "5"
},
{
"brandName": "ruggers",
"brandId": "4"
}
],
"stores": [
{
"storeName": "peter england",
"storeId": "3"
},
{
"storeName": "peter england",
"storeId": "4"
}
],
"filters": {
"0": {
"filter": "M",
"filterId": "11"
},
"1": {
"filter": "S",
"filterId": "12"
},
"filterName": "size"
}
}
my php code is below
$dbFactory = new DBFactory();
$jsonArray = array();
if ($subcatid != '')
{
$p2 = $dbFactory->sel_product3($subcatid, $area_value);
if (count($p2) > 0)
{
for ($j = 0; $j < count($p2); $j++)
{
$p3 = $dbFactory->sel_store($p2[$j]['STORE_ID']);
$temp['offers'][] = array(
"offerId" => $p2[$j]['P_ID'],
"offerName" => $p2[$j]['P_NAME'],
"image" => $url . $p2[$j]['P_ID'] . '.jpg',
"brandId" => $p2[$j]['BRAND_ID'],
"storeId" => $p2[$j]['STORE_ID'],
"price" => $p2[$j]['PRICE'],
"offer" => $p2[$j]['OFFER'],
"storeName" => $p3[0]['S_NAME']
);
$jsonArray = $temp;
}
}
}
// filters code=================
/*code to retrive brnads and stores goes here*/
foreach($res as $value)
{
$br = $dbFactory->sel_brand($value);
$temp["brands"][] = array("brandName" => $br[0]['BRAND_NAME'] ,"brandId"=>$br[0]['BID'] );
$jsonArray = $temp;
}
foreach($store_value as $value)
{
$sr = $dbFactory->sel_store($value);
$temp["stores"][] = array("storeName" => $sr[0][S_NAME] ,"storeId"=>$sr[0][S_ID] );
$jsonArray = $temp;
}
$parentCatId = $dbFactory->getparentcatId($subcatid);
$fg = $dbFactory->get_filtergroup($parentCatId[0]['PARENT_ID']);
for ($i = 0; $i < count($fg); $i++)
{
$f = $dbFactory->sel_filter($fg[$i][F_ID]);
$temp['filters'] = array();
$temp['filters']['filterName'] = $fg[$i][FNAME];
for ($j = 0; $j < count($f); $j++)
{
$temp['filters'][] = array("filter"=>$f[$j][FNAME],"filterId"=>$f[$j][F_ID] );
}
array_push($jsonArray, $temp);
}
$jsonArray = $temp;
echo json_encode($jsonArray);
tried a lot to get the desired json format but failed.please help me out to solve this problem
I'm generating the following array in json format
[
{
"country": "China",
"amount": "1"
},
{
"country": "India",
"amount": "5"
},
{
"country": "India",
"amount": "317"
},
{
"country": "India",
"amount": "8"
},
{
"country": "India",
"amount": "2"
},
{
"country": "United States",
"amount": "213"
},
{
"country": "Iceland",
"amount": "263"
}
]
I've tried merging them with the following code
$newData = array();
$result = array_reduce(
$data,
function($newData, $value) {
$key = $value['country'];
if (!isset($newData[$key])) {
$newData[$key] = $value;
} else {
$newData[$key]['amount'] += $value['amount'];
}
return $newData;
},
$newData
);
my desired output is
[
{
"country": "China",
"amount": "1"
},
{
"country": "India",
"amount": "332"
},
{
"country": "United States",
"amount": "213"
},
{
"country": "Iceland",
"amount": "263"
}
]
As you can see the array is merged with all the country values grouped and the amount values added accordingly.
Use a simple foreach loop rather than array_reduce.
$newData = array();
foreach ($data as $e) {
if (isset($newData[$e['country']])) {
$newData[$e['country']]['amount'] += $e['amount'];
} else {
$newData[$e['country']] = $e;
}
}
$newData = array_values($newData);
Try this:
$newData = array();
foreach($oldData as $oldEntry){
foreach($newData as $newEntry){
if($oldEntry['country'] == $newEntry['country'])
$newEntry['country'] += $oldEntry['country'];
break;
}
$newData[] = array('country' => $oldEntry['country'], 'amount' => $oldEntry['amount'];
}
I have this JSON code:
{
"phrases": [
{
"phrases": [
{
"id": "33",
"text": "sasdsad",
"date": "2012-03-14 20:28:45",
"views": "0",
"ip": "64.191.90.5",
"reported": "0",
"strange": "0",
"lang": "en"
},
{
"id": "32",
"text": "que ondaa\r\n",
"date": "2012-03-14 20:27:45",
"views": "0",
"ip": "64.191.90.5",
"reported": "0",
"strange": "0",
"lang": "en"
},
{
"id": "31",
"text": "dsadssadsad",
"date": "2012-03-14 20:27:35",
"views": "0",
"ip": "64.191.90.5",
"reported": "0",
"strange": "0",
"lang": "en"
}
],
"details": {
"success": "true",
"phrase_id": "",
"phrase_text": "",
"phrase_date": ""
}
}
I don't really know what to do. I get some phrases vía MySQL, and pushes them to an array. This array is json_encoded() and gets printed.
$sth = $sql;
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
$sth = $sql;
$data = array(
"success" => "true",
"phrase_id" => "",
"phrase_text" => "",
"phrase_date" => "",
);
print json_encode($rows).json_encode($data);
and with jQuery I was trying to parse it, but I can't. This is the main problem.
function getPhrases(order,limit,last){
var req_url = ...;
$.getJSON(req_url, function(data) {
$.each(data.phrases, function(i, data) {
appendPhrase(data.text);
lastid = data.id;
});
$.each(data.details, function(i, data) {
$("#phrases-count").html(data.totalcount);
});
});
}
PS: I was doing this with "echo" but got some problems.
{
"phrases": [
{
"id": "33",
"text": "sasdsad",
"date": "2012-03-14 20:28:45",
"views": "0",
"ip": "64.191.90.5",
"lang": "en"
},
{
"id": "32",
"text": "que ondaa<br />",
"date": "2012-03-14 20:27:45",
"views": "0",
"ip": "64.191.90.5",
"lang": "en"
},
{
"id": "31",
"text": "dsadssadsad",
"date": "2012-03-14 20:27:35",
"views": "0",
"ip": "64.191.90.5",
"lang": "en"
}
],
"details": [
{
"totalcount": "3",
"logged_in": "false"
}
]
}
You can't simply combine these JSON arrays:
print json_encode($rows).json_encode($data);
Try this (attempt 2):
print json_encode( array('phrases' => $rows, 'details' => $data) );