Page 1 of 2

how to match the values between two json response and print

Posted: Sat Jan 09, 2016 11:50 am
by sathya
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

Re: how to match the values between two json response and pr

Posted: Sat Jan 09, 2016 12:03 pm
by Christopher
Do you want the logic to deal with having Fullname in one response, but not having Fullname in the other?

Re: how to match the values between two json response and pr

Posted: Sat Jan 09, 2016 12:09 pm
by sathya
Yes exactly.

Re: how to match the values between two json response and pr

Posted: Sat Jan 09, 2016 12:13 pm
by sathya
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

Posted: Sun Jan 10, 2016 11:26 am
by sathya
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

Re: how to match the values between two json response and pr

Posted: Sun Jan 10, 2016 9:37 pm
by Christopher
What code do you have so far? Please post the code you have written.

Re: how to match the values between two json response and pr

Posted: Mon Jan 11, 2016 6:52 am
by sathya
Hi ,i have posted my code here.

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>

output:

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..

Re: how to match the values between two json response and pr

Posted: Mon Jan 11, 2016 6:58 am
by sathya
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.

Re: how to match the values between two json response and pr

Posted: Mon Jan 11, 2016 1:02 pm
by Christopher
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:

Code: Select all

    foreach($json['stations'] as $stop) {
Also, I think this line should be:

Code: Select all

       $stop['has_arrived'] = $stop['has_arrived'] === true ? "Arrived" : "Still not here yet!";

Re: how to match the values between two json response and pr

Posted: Mon Jan 11, 2016 8:54 pm
by sathya
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

Re: how to match the values between two json response and pr

Posted: Wed Jan 13, 2016 6:57 am
by sathya
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.

Re: how to match the values between two json response and pr

Posted: Wed Jan 13, 2016 11:30 am
by Christopher
sathya wrote:i want to pass the iterated array value using forloop into the url (http://example.com/arrayvalue/)
Do you want to loop through stations or route? For example, if it is route then:

Code: Select all

foreach ($response.route as $route) {
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.
What values do you want in the URL?

Re: how to match the values between two json response and pr

Posted: Wed Jan 13, 2016 11:44 am
by sathya
I want the station code in the url

Re: how to match the values between two json response and pr

Posted: Wed Jan 13, 2016 11:52 am
by sathya
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

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]";
  }
When I passed the stations code one by one I have to get the stations name and print

Re: how to match the values between two json response and pr

Posted: Wed Jan 13, 2016 3:57 pm
by Christopher
So what does this output? Does $stop['station'] have the data you need?

And again, this is preferable:

Code: Select all

 $stop['has_arrived'] = $stop['has_arrived'] === true ? "Arrived" : "Still not here yet!";
Or better not changing data:

Code: Select all

 $stop['message'] = $stop['has_arrived'] === true ? "Arrived" : "Still not here yet!";