I want to control the data coming with the post with the function and update it if there is data, and add it if there is no data.
I am updating and adding with the following codes, but in the second addition, it both updates the existing information and adds the same information again.
I wonder where am I doing the mistake, can you help me?
$data = array('sure'=>'1', 'id'=>'38138373','t'=>'6', 's'=>'s3', 'p'=>'6');
function updateCart($data,$user_id,$cartid){
$updateCart = [];
$carts = getCart($user_id,$cartid);
foreach ($carts as $i => $cart) {
if($cart['id'] == $data['id'] && $cart['t'] == $data['t'] && $cart['s'] == $data['s']) {
$newp = $cart['p']+$data['p'];
$carts[$i]['p'] = $newp;
} else {
$carts[] = $data;
}
}
putJson($carts,$user_id,$cartid);
}
/* $carts */
[
{
"sure": "1",
"id": "38138373",
"t": "8",
"s": "s5",
"p": 6
},{
"sure": "1",
"id": "38138373",
"t": "6",
"s": "s3",
"p": 9
}
]
This is the result after the function.
[
{
"sure": "1",
"id": "38138373",
"t": "8",
"s": "s5",
"p": 6
},
{
"sure": "1",
"id": "38138373",
"t": "6",
"s": "s3",
"p": 9
},
{
"sure": "1",
"id": "38138373",
"t": "6",
"s": "s3",
"p": 12
},
{
"sure": "1",
"id": "38138373",
"t": "6",
"s": "s3",
"p": "3"
}
]
The file should be like this
[
{
"sure": "1",
"id": "38138373",
"t": "8",
"s": "s5",
"p": 6
},
{
"sure": "1",
"id": "38138373",
"t": "6",
"s": "s3",
"p": 15
}
]
The below code should work for you:
function updateCart($data, $user_id, $cartid){
$updateCart[] = $data;
$carts = getCart($data, $user_id, $cartid);
foreach ($carts as $cart) {
if( ($cart['id'] == $data['id']) && ($cart['t'] == $data['t']) &&
($cart['s'] == $data['s'])) {
$cart['p'] = $data['p'] + $cart['p'];
$data['p'] = $cart['p'];
$updateCart[0] = $cart;
}
$updateCart[] = $cart;
}
$updateCart[] = array_shift($updateCart);
return putJson($updateCart, $user_id, $cartid);
}
Let me know if this is okay. You should try and add break points at key stages in your code, to examine if the output is correct. How you do this is up to you, and your choices will vary depending on the framework you use etc. You could start with reading the PHP documentation about var_dump here.
Could you give me some advice how to get data from this file?
I need to receive, for example, a list of stops and lines, or other data from the most nested objects, but I don't now how to do that with nested objects in JSON.
{
"stops": {
"11": "Winna-Karpacka",
"21": "Piwna",
"31": "RondoTysiąclecia",
"41": "Piątkowicka-Wiączyńska",
"51": "Taczańska",
"61": "Komarzewska",
"12": "Winna-Działki",
"22": "Piwna-Karpacka",
"32": "RondoTysiąclecia",
"42": "Piątkowicka",
"52": "Taczańska",
"62": "Komarzewska-Wiączyńska"
},
"lines": {
"1": {
"variants": {
"16": [{
"11": 0
}, {
"21": 3
}, {
"31": 5
}, {
"41": 10
}, {
"51": 12
}, {
"61": 16
}],
"61": [{
"62": 0
}, {
"52": 3
}, {
"42": 6
}, {
"32": 8
}, {
"22": 14
}, {
"12": 16
}]
},
"departures": {
"16": [
"04:00",
"04:50",
"05:40",
"06:30",
"07:20",
"08:10",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"14:50",
"15:40",
"16:30",
"17:20",
"18:10",
"19:00",
"20:00",
"21:00",
"22:00"
],
"61": [
"04:25",
"05:15",
"06:05",
"06:55",
"07:45",
"08:35",
"09:30",
"10:30",
"11:30",
"12:30",
"13:30",
"14:25",
"15:15",
"16:05",
"16:55",
"17:45",
"18:35",
"19:30",
"20:30",
"21:30"
]
}
},
"2": {
"variants": {
"25": [{
"21": 0
}, {
"31": 2
}, {
"41": 7
}, {
"51": 9
}],
"52": [{
"52": 0
}, {
"42": 3
}, {
"32": 5
}, {
"22": 11
}]
},
"departures": {
"25": [
"10:15",
"10:45",
"11:15",
"11:45",
"12:15",
"12:45",
"13:15",
"13:45",
"14:15",
"14:45",
"15:15",
"15:45",
"16:15",
"16:45",
"17:15",
"17:45"
],
"52": [
"10:30",
"11:00",
"11:30",
"12:00",
"12:30",
"13:00",
"13:30",
"14:00",
"14:30",
"15:00",
"15:30",
"16:00",
"16:30",
"17:00",
"17:30"
]
}
},
"3": {
"variants": {
"35": [{
"31": 0
}, {
"41": 12
}, {
"51": 22
}],
"53": [{
"51": 0
}, {
"41": 10
}, {
"31": 22
}]
},
"departures": {
"34": [
"01:10",
"02:10",
"03:10"
],
"43": [
"01:40",
"02:40",
"03:40"
]
}
}
}
}
I tried something like this but it doesn't work
$str = file_get_contents('dane.json');
$json = json_decode($str, true);
echo $json['stops']['11'];
json_decode, then use this function I wrote to generate the appropriate PHP code to access any specific element in your JSON.
function jsonGeneratePhp($json, $phpString = '$json') {
if (is_array($json)) {
echo "$phpString<br>";
foreach ($json as $key => $value) {
jsonGeneratePhp($value, "{$phpString}['$key']");
}
} elseif (is_object($json)) {
echo "$phpString<br>";
foreach ($json as $key => $value) {
if (is_numeric($key)) {
jsonGeneratePhp($value, "{$phpString}->{{$key}}");
} else {
jsonGeneratePhp($value, "{$phpString}->$key");
}
}
} else {
echo "$phpString = $json<br>";
}
}
$str = file_get_contents('dane.json');
$json = json_decode($str);
jsonGeneratePhp($json);
This will generate a bunch of PHP statements that correspond to each piece of your JSON data, like this:
$json
$json->stops
$json->stops->{11} = Winna-Karpacka
$json->stops->{21} = Piwna
...
Anything that ends in an = something is a value.
for example: echo $json->stops->{11}; // Winna-Karpacka
Anything that does not end in an = something is an array or an object that you can iterate with foreach.
for example: foreach ($json->stops as $stopId => $stopName) { ... }
I'm developing website using weatherapi. I intend to fetch weather using cityid which is in citylist.json file. Users wil enter city name, I was hoping to extract cityid from this json and use the cityid to fetch weather using api. But there is some error in json.
if (file_exists('citylist.json')) {
$cityArray = file_get_contents("citylist.json");
$cityAA = json_decode($cityArray,true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
print_r($cityAA);
}
The json is around 12mb. Here are first few lines
{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
},
{
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}{
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
},
{
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}
I've tried jsonlint but the file is too big I guess.
The var_dump suggests Syntax error, malformed JSON. I cannot post the image as i'm not allowed.
How can I find out where the json is malformed?
This site can help you: http://jsonlint.com/
In the example provided you can't just pass 2 objects like you did. You need to put it inside an array.
[obj,obj2]
Fixed:
[{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
}, {
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}, {
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
}, {
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}]
It looks like the file contains multiple JSON objects but not well integrated.
This is valid JSON:
{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
},
{
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}
And this is too:
{
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
},
{
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}
But how they are put together, it is not. That might be a problem of the JSON file imported or if you are generating it by yourself from the API.
A valid JSON would be formed like an array of those objects:
[{
"_id": 14256,
"coord": {
"lon": 48.570728,
"lat": 34.790878
},
"country": "IR",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 132142
},
"langs": [{
"de": "Azad Shahr"
}, {
"fa": "آزادشهر"
}],
"name": "Azadshahr",
"stat": {
"level": 1.0,
"population": 514102
},
"stations": [{
"id": 7030,
"dist": 9,
"kf": 1
}],
"zoom": 10
}, {
"_id": 18918,
"coord": {
"lon": 34.058331,
"lat": 35.012501
},
"country": "CY",
"geoname": {
"cl": "P",
"code": "PPL",
"parent": 146615
},
"langs": [{
"en": "Protaras"
}, {
"ru": "Протарас"
}],
"name": "Protaras",
"stat": {
"level": 1.0,
"population": 20230
},
"stations": [{
"id": 5448,
"dist": 42,
"kf": 1
}],
"zoom": 6
}]
PS: You can use http://jsonlint.com/ to check valid JSON.
I tried to convert the json file into an array of multiple json snippets using the below function
function json_split_objects($json){
$q = FALSE;
$len = strlen($json);
for($l=$c=$i=0;$i<$len;$i++)
{
$json[$i] == '"' && ($i>0?$json[$i-1]:'') != '\\' && $q = !$q;
if(!$q && in_array($json[$i], array(" ", "\r", "\n", "\t"))){continue;}
in_array($json[$i], array('{', '[')) && !$q && $l++;
in_array($json[$i], array('}', ']')) && !$q && $l--;
(isset($objects[$c]) && $objects[$c] .= $json[$i]) || $objects[$c] = $json[$i];
$c += ($l == 0);
}
return $objects;
}
if (file_exists('current_cities.json')) {
echo "here";
$cityJson = file_get_contents("current_cities.json");
$city_json_data_array = json_split_objects($cityJson);
But this did not work, below was the split array generated but it did not put comma(,) between the snippets.
Array
(
[0] => {"_id":14256,"coord":{"lon":48.570728,"lat":34.790878},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":132142},"langs":[{"de":"Azad Shahr"},{"fa":"آزادشهر"}],"name":"Azadshahr","stat":{"level":1.0,"population":514102},"stations":[{"id":7030,"dist":9,"kf":1}],"zoom":10}
[1] => {"_id":18918,"coord":{"lon":34.058331,"lat":35.012501},"country":"CY","geoname":{"cl":"P","code":"PPL","parent":146615},"langs":[{"en":"Protaras"},{"ru":"Протарас"}],"name":"Protaras","stat":{"level":1.0,"population":20230},"stations":[{"id":5448,"dist":42,"kf":1}],"zoom":6}
[2] => {"_id":23814,"coord":{"lon":47.055302,"lat":34.383801},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":128222},"langs":[{"fa":"کهریز"}],"name":"Kahriz","stat":{"level":1.0,"population":766706},"stations":[{"id":7022,"dist":10,"kf":1}],"zoom":7}
[3] => {"_id":24851,"coord":{"lon":47.9725,"lat":34.073399},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":125605},"langs":[{"fa":"نور آباد"},{"link":"http://en.wikipedia.org/wiki/Nurabad%2C_Lorestan"},{"ru":"Нурабад"}],"name":"Nurabad","stat":{"level":1.0,"population":73528},"stations":[{"id":7022,"dist":80,"kf":1},{"id":7024,"dist":75,"kf":1},{"id":7073,"dist":49,"kf":1}],"zoom":9}
[4] => {"_id":32723,"coord":{"lon":52.309422,"lat":35.23455},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":116401},"langs":[{"fa":"ايستگاه گرمسار"}],"name":"Istgah-e Garmsar","stat":{"level":1.0,"population":49491},"stations":[{"id":7036,"dist":99,"kf":1}],"zoom":8}
[5] => {"_id":32767,"coord":{"lon":51.56889,"lat":35.439442},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":110791},"langs":[{"fa":"قرچك"}],"name":"Qarchak","stat":{"level":1.0,"population":251834},"stations":[{"id":7032,"dist":36,"kf":1},{"id":7074,"dist":48,"kf":1}],"zoom":9}
[6] => {"_id":41210,"coord":{"lon":49.195999,"lat":36.213001},"country":"IR","geoname":{"cl":"P","code":"PPL","parent":111452},"langs":[{"fa":"خرم درّه"}],"name":"Khorram Darreh","stat":{"level":1.0,"population":50528},"stations":[{"id":7033,"dist":76,"kf":1}],"zoom":12}
[7] => {"_id":50672,"coord":{"lon":44.893799,"lat":2.6185},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":51966},"langs":[{"so":"Wanlaweyn"}],"name":"Wanlaweyn","stat":{"level":1.0,"population":22022},"zoom":9}
[8] => {"_id":52867,"coord":{"lon":44.529991,"lat":1.78784},"country":"SO","geoname":{"cl":"P","code":"PPLA2","parent":51966},"langs":[{"so":"Qoryooley"}],"name":"Qoryooley","stat":{"level":1.0,"population":51720},"zoom":8}
[9] => {"_id":53157,"coord":{"lon":49.872822,"lat":11.47197},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":64661},"langs":[{"link":"http://en.wikipedia.org/wiki/Qandala"},{"so":"Qandala"}],"name":"Qandala","stat":{"level":1.0,"population":15923},"zoom":8}
[10] => {"_id":53654,"coord":{"lon":45.34375,"lat":2.03711},"country":"SO","geoname":{"cl":"P","code":"PPLC","parent":64833},"name":"Mogadishu","stat":{"level":1.0,"population":2587183},"zoom":1}
[11] => {"_id":54715,"coord":{"lon":42.544498,"lat":3.79376},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":58802},"name":"Luuq","stat":{"level":1.0,"population":33820},"zoom":8}
[12] => {"_id":55671,"coord":{"lon":42.545361,"lat":-0.35817},"country":"SO","geoname":{"cl":"P","code":"PPLA","parent":56083},"langs":[{"de":"Kismaayo"},{"en":"Kismayo"},{"es":"Kismaayo"},{"fi":"Kismayo"},{"fr":"Kismaayo"},{"id":"Kismaayo"},{"ja":"キスマヨ"},{"link":"http://en.wikipedia.org/wiki/Kismayo"},{"nl":"Kismayo"},{"so":"Kismaayo"},{"sw":"Kismayu"}],"name":"Kismaayo","stat":{"level":1.0,"population":234852},"zoom":6}
[13] => {"_id":56166,"coord":{"lon":42.785351,"lat":0.48829},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":56084},"langs":[{"link":"http://en.wikipedia.org/wiki/Jilib"},{"ru":"Джилиб"}],"name":"Jilib","stat":{"level":1.0,"population":43694},"zoom":9}
[14] => {"_id":56335,"coord":{"lon":45.500481,"lat":2.78087},"country":"SO","geoname":{"cl":"P","code":"PPLA","parent":51967},"langs":[{"de":"Giohar"},{"en":"Giohar"},{"link":"http://en.wikipedia.org/wiki/Jowhar"},{"so":"Jawhar"}],"name":"Jawhar","stat":{"level":1.0,"population":47086},"zoom":8}
[15] => {"_id":56399,"coord":{"lon":42.744968,"lat":0.06968},"country":"SO","geoname":{"cl":"P","code":"PPL","parent":56083},"langs":[{"de":"Jamaame"},{"en":"Jamaame"},{"link":"http://en.wikipedia.org/wiki/Jamame"}],"name":"Jamaame","stat":{"level":1.0,"population":185270},"zoom":6}
I was thinking of fetching the "_id" using the $city variable which the user enters
for($i = 0, $size = count($city_json_data_array); $i < $size; ++$i) {
if ($city_json_data_array[$i].['name'] == $city){
$cityId = $city_json_data_array[$i]['_id'];
print_r($cityId);
}
}
So the JSON file was indeed not correct, I had to use str_replace() and write the file using file_put_contents() to create a new valid json file.
This is my working code
if (isset($_GET['city']) && $_GET['city']){
$city = ucwords($_GET['city']);
if (file_exists('currentCityArray.json')) {
$cityArray = file_get_contents("currentCityArray.json");
$decodedArray="";
$decodedArray = json_decode($cityArray, true);
//print_r($decodedArray);
//Search the $decodedArray to match city name entered by the user and fetch the cityID for that city
for($i = 0, $size = count($decodedArray);$i<$size; $i++) {
if ($decodedArray[$i]['name'] == $city){
$cityId = $decodedArray[$i]['_id'];
}
}
if(!$cityId){
$error .= $city." city could not be found in our database. Please enter a valid city name.";
}else{
//Fetch the weather contents by using $cityId
$urlContents = file_get_contents("http://api.openweathermap.org/data/2.5/forecast?id=".urlencode($cityId)."&APPID=0934a70098e84dc720b8d7f07bb1202d");
// added a flag 'true' to retrieve the data in $weatherArray as associative array
$weatherArray = json_decode($urlContents, true);
//print_r ($weatherArray);
if($weatherArray['cod']=="200"){
//print_r($weatherArray);
$weatherInfo = "The weather in ".$city." is currently '".$weatherArray['list'][0]['weather'][0]['description']."'";
$tempCelcius = intval($weatherArray['list'][0]['main']['temp'] - 273);
$weatherInfo .= ". The average temperature is expected to be about: ".$tempCelcius."°C";
$windSpeed = $weatherArray['list'][0]['wind']['speed'];
$weatherInfo .= ". The wind speed is currently ".$windSpeed."m/s.";
}else{
$error .= $city." city could not be found in our database. Please enter a valid city name.";
}
}
}else{
$error .= "Crikey ! the file with the list of cities has gone missing!";
}
}