how to match the values between two json response and print
Moderator: General Moderators
how to match the values between two json response and print
Hi All,
I have two json response.
Json. Response 1:sample thing I am writing here.
Example Domain
District:
Name=Nmkl
Name=KRR
Name=SLM
Name=MAS
Json Response 2:
Example Domain
Name=Nmkl
Fullname=Namakkal
Name=KRR
Fullname=Karur
Name=SLM
Fullname=Salem
Name=MAS
Fullname=Madras
Here what's my problem is , when I pass the Name field in json response 2 then only i Will get the response.
For example if I pass the Nmkl from json response 1 to Json Response 2,I will get Name as Nmkl and Fullname as Namakkal
This thing I have to do in foreach loop to get the all the values
I am very much confused about writing the logic .
could somebody help me to get this.
Thanks
I have two json response.
Json. Response 1:sample thing I am writing here.
Example Domain
District:
Name=Nmkl
Name=KRR
Name=SLM
Name=MAS
Json Response 2:
Example Domain
Name=Nmkl
Fullname=Namakkal
Name=KRR
Fullname=Karur
Name=SLM
Fullname=Salem
Name=MAS
Fullname=Madras
Here what's my problem is , when I pass the Name field in json response 2 then only i Will get the response.
For example if I pass the Nmkl from json response 1 to Json Response 2,I will get Name as Nmkl and Fullname as Namakkal
This thing I have to do in foreach loop to get the all the values
I am very much confused about writing the logic .
could somebody help me to get this.
Thanks
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: how to match the values between two json response and pr
Do you want the logic to deal with having Fullname in one response, but not having Fullname in the other?
(#10850)
Re: how to match the values between two json response and pr
Yes exactly.I want the logic or some code example to explain this,this will help me a lot.I am new to php .
Re: how to match the values between two json response and pr
Kindly any one provide the logic or idea to make the code.I am middle of wall and getting confused.
If u have queries feel free to post here
Thanks
If u have queries feel free to post here
Thanks
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: how to match the values between two json response and pr
What code do you have so far? Please post the code you have written.
(#10850)
Re: how to match the values between two json response and pr
Hi ,i have posted my code here.
Livetrainstatus.php code:
output:
here it will produce the station,scheduled arrival,actual arrival,arrived status.
Json Response:
This is the url format for getting the above api response.
fullname (code) ,scheduled arrival,actual arrival,arrived status
can anyone suggest the code or logic..
Livetrainstatus.php code:
Code: Select all
<!DOCTYPE html>
<HTML>
<HEAD>
</head>
<body>
<?php
$json = '';
if(isset($_GET['trainnumber']) && (isset($_GET['doj']))) //passing the train number and doj from home page to here
{
$url = 'http://api.railwayapi.com/live/train/'.$_GET['trainnumber'].'/doj/'.$_GET['doj'].'/apikey/*/'; //api url
$json = json_decode(file_get_contents($url), true);
echo "<center>Station<center><center>Scheduled<center>center>Actual<center><center>Status/Delay";
foreach($json['route'] as $stop) {
($stop['has_arrived'] === true) ? $stop['has_arrived'] = "Arrived" : $stop['has_arrived'] = "Still not here yet!";
echo ' <center>'.$stop['station'].'<center>'.$stop['scharr'].'<center>'.$stop['actarr'].'<center>'.$stop['has_arrived']."\n";
}
} else {
echo "Something went wrong, please notify to admin [support@livetrainrunningstatus.co.in]";
}
?>
</BODY>
</HTML>
here it will produce the station,scheduled arrival,actual arrival,arrived status.
Json Response:
{
"response_code": 200,
"stations": [
{
"lng": 83.3731675,
"lat": 26.7605545,
"code": "GKP",
"state": "Uttar Pradesh",
"fullname": "Gorakhpur Jn"
},
{
"lng": 83.5842775,
"lat": 26.6492695,
"code": "CC",
"state": "Uttar Pradesh",
"fullname": "Chauri Chaura"
},
{
"lng": 83.2142269,
"lat": 26.758544,
"code": "SWA",
"state": "Uttar Pradesh",
"fullname": "Sahjanwa"
},
{
"lng": 83.3742644,
"lat": 26.7401226,
"code": "DDP",
"state": "Uttar Pradesh",
"fullname": "Daudpur"
}
]
}
This is the url format for getting the above api response.
i want to pass all the station name from livetrainstatus.php to this url and make the output as
fullname (code) ,scheduled arrival,actual arrival,arrived status
can anyone suggest the code or logic..
Last edited by requinix on Wed Feb 03, 2016 11:37 pm, edited 1 time in total.
Reason: removing api key by request
Reason: removing api key by request
Re: how to match the values between two json response and pr
i have to pass the each station code inside the api url and get the result and decode then i have to print all the full name for that station code.
i dont know how to make the code for this.kindly help me.
i dont know how to make the code for this.kindly help me.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: how to match the values between two json response and pr
Looking at the JSON returned, I do not see how you can determine whether a train has arrived. That seems to be what you are trying to determine.
It seems like for the JSON returned, the loop should be:
Also, I think this line should be:
It seems like for the JSON returned, the loop should be:
Code: Select all
foreach($json['stations'] as $stop) {
Code: Select all
$stop['has_arrived'] = $stop['has_arrived'] === true ? "Arrived" : "Still not here yet!";
(#10850)
Re: how to match the values between two json response and pr
Yes already I did this in my code.in my json response i will get the arrived status as boolean value so based on that boolean values I haved coded using ternary operator.I know it's printing properly.if u want to check the url pls navigate to livetrainrunningstatus.co.in and enter the train number as 22651 else 22652
I think you got clear.my problem is not about arrived status here.but anyway I have to said clearly.
I can get all the stations from the below json response:
{"response_code":200,"error":"","position":"Train has reached Destination and late by 5 minutes.","train_number":"12046","route":[{"no":1,"station":"CDG","has_arrived":false,"has_departed":true,"day":0,"distance":0,"scharr":"Source","schdep":"12:00","actarr":"00:00","actdep":"12:00","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":0},{"no":2,"station":"UMB","has_arrived":true,"has_departed":true,"day":0,"distance":67,"scharr":"12:40","schdep":"12:42","actarr":"12:40","actdep":"12:42","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":0},{"no":3,"station":"KUN","has_arrived":true,"has_departed":true,"day":0,"distance":142,"scharr":"13:36","schdep":"13:38","actarr":"13:57","actdep":"13:59","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":21},{"no":4,"station":"NDLS","has_arrived":true,"has_departed":false,"day":0,"distance":265,"scharr":"15:25","schdep":"Destination","actarr":"15:30","actdep":"00:00","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":5}]}
Initially I will get the results from the above json response only
Here I want to interate the stations into the api url as mentioned already then I have to print like gorakphur jn (gkp)
Thanks for your replies
I think you got clear.my problem is not about arrived status here.but anyway I have to said clearly.
I can get all the stations from the below json response:
{"response_code":200,"error":"","position":"Train has reached Destination and late by 5 minutes.","train_number":"12046","route":[{"no":1,"station":"CDG","has_arrived":false,"has_departed":true,"day":0,"distance":0,"scharr":"Source","schdep":"12:00","actarr":"00:00","actdep":"12:00","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":0},{"no":2,"station":"UMB","has_arrived":true,"has_departed":true,"day":0,"distance":67,"scharr":"12:40","schdep":"12:42","actarr":"12:40","actdep":"12:42","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":0},{"no":3,"station":"KUN","has_arrived":true,"has_departed":true,"day":0,"distance":142,"scharr":"13:36","schdep":"13:38","actarr":"13:57","actdep":"13:59","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":21},{"no":4,"station":"NDLS","has_arrived":true,"has_departed":false,"day":0,"distance":265,"scharr":"15:25","schdep":"Destination","actarr":"15:30","actdep":"00:00","scharr_date":"19 Nov 2015","actarr_date":"19 Nov 2015","latemin":5}]}
Initially I will get the results from the above json response only
Here I want to interate the stations into the api url as mentioned already then I have to print like gorakphur jn (gkp)
Thanks for your replies
Re: how to match the values between two json response and pr
you can provide any one example for me in the below scenario,
i want to pass the iterated array value using forloop into the url (http://example.com/arrayvalue/)
then i will get the some json response from that url.
i have to decode all the response using again for loop and print all the values which got via the url.
i want to pass the iterated array value using forloop into the url (http://example.com/arrayvalue/)
then i will get the some json response from that url.
i have to decode all the response using again for loop and print all the values which got via the url.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: how to match the values between two json response and pr
Do you want to loop through stations or route? For example, if it is route then:sathya wrote:i want to pass the iterated array value using forloop into the url (http://example.com/arrayvalue/)
Code: Select all
foreach ($response.route as $route) {What values do you want in the URL?sathya wrote:then i will get the some json response from that url.
i have to decode all the response using again for loop and print all the values which got via the url.
(#10850)
Re: how to match the values between two json response and pr
I want the station code in the url
Re: how to match the values between two json response and pr
I want the station code in the url,but the station code will come from the below for each loop
Please use the PHP Code tag for code
When I passed the stations code one by one I have to get the stations name and print
Please use the PHP Code tag for code
Code: Select all
foreach($json['route'] as $stop) {
($stop['has_arrived'] === true) ? $stop['has_arrived'] = "Arrived" : $stop['has_arrived'] = "Still not here yet!";
echo ' <center>'.$stop['station'].'<center>'.$stop['scharr'].'<center>'.$stop['actarr'].'<center>'.$stop['has_arrived']."\n";
}
} else {
echo "Something went wrong, please notify to admin [support@livetrainrunningstatus.co.in]";
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: how to match the values between two json response and pr
So what does this output? Does $stop['station'] have the data you need?
And again, this is preferable:
Or better not changing data:
And again, this is preferable:
Code: Select all
$stop['has_arrived'] = $stop['has_arrived'] === true ? "Arrived" : "Still not here yet!";Code: Select all
$stop['message'] = $stop['has_arrived'] === true ? "Arrived" : "Still not here yet!";(#10850)