$json=Array ( [response_code] => 200
[stations] => Array (
[0] => Array ( [lat] => 13.0848031Code: Select all
=> MAS [lng] => 80.2749909 [fullname] => CHENNAI CENTRAL [state] => Tamil Nadu ) [1] => Array ( [lat] => 13.0778142 [code] => MS [lng] => 80.2596615 [fullname] => CHENNAI EGMORE [state] => Tamil Nadu ) [2] => Array ( [lat] => 12.9229153 [code] => TBM [lng] => 80.1274558 [fullname] => TAMBARAM [state] => Tamil Nadu ) [3] => Array ( [lat] => 13.1160178 [code] => PER [lng] => 80.2316646 [fullname] => PERAMBUR [state] => Tamil Nadu ) [4] => Array ( [lat] => 13.0373769 [code] => ASKN [lng] => 80.2122821 [fullname] => ASHOK NAGAR [state] => Tamil Nadu ) [5] => Array ( [lat] => 13.037713 [code] => MBM [lng] => 80.2276873 [fullname] => MAMBALAM [state] => Tamil Nadu ) [6] => Array ( [lat] => 13.1067448 [code] => AVD [lng] => 80.0969511 [fullname] => AVADI [state] => Tamil Nadu ) ) ); [/quote] i have tried the below php code but got the output as all the full names in the above array but i want only [fullname] => CHENNAI CENTRAL [syntax=php]foreach($jsont['stations'] as $station_fullname) { echo ' <TR><TD>'.$station_fullname['fullname'].'</TD></TR>\n"; } } } else { echo 'Something went wrong, please notify to admin '; } [/syntax] can any one help me to get the solution?
How to get the specific element value from associative array
Moderator: General Moderators
How to get the specific element value from associative array
the below code have associative array and i want to get the first element in the array as [fullname] => CHENNAI CENTRAL only with forach loop.
Re: How to get the specific element value from associative a
$jsont is typo ,actually its $json
Re: How to get the specific element value from associative a
Code: Select all
$first = array_shift($json['stations']); // alternately, $json['stations'][0]
print $first['name'];Re: How to get the specific element value from associative a
in the below code what $first will do here and want to know how to get the full name of the first station only.
$first = array_shift($json['stations']); // alternately, $json['stations'][0]
print $first['name'];
$first = array_shift($json['stations']); // alternately, $json['stations'][0]
print $first['name'];
Re: How to get the specific element value from associative a
Typo. The array key is fullname, not name. Apologies.
Re: How to get the specific element value from associative a
Sorry the output i am getting is very wrong.also its printing all the stationsCelauran wrote:Typo. The array key is fullname, not name. Apologies.
i have many associative arrays i want to get only the first element in loop [fullname] => CHENNAI CENTRAL like that for the below associative arrayPALANI CHATRAPPATTI TIRUTTANGAL SIVAKASI SRIVILLIPUTTUR ODDANCHATRAM AKKARAIPPATTI SALEM JN SALEM TOWN SANKARIDURG RASIPURAM DINDIGUL JN AMBATURAI KODAIKANAL ROAD KARUR PUGALUR MAHADANAPURAM MOHANUR MOHANUR KARUR PUGALUR NAMAKKAL MAHADANAPURAM NAMAKKAL PUGALUR MOHANUR RASIPURAM SALEM JN AYODHYAPATTANAM SALEM TOWN AKKARAIPPATTI SALEM JN OMALUR AYODHYAPATTANAM SALEM TOWN RASIPURAM AKKARAIPPATTI JOLARPETTAI VANIYAMBADI TIRUPATTUR JN GUDIYATTAM AMBUR KATPADI JN VELLORE CANT RAM NAGAR J K MUKUNDARAYAPURM RAM CHAURA ROAD ARAKKONAM TIRUTTANI ANAVARDIKHANPET PERAMBUR CHENNAI EGMORE TAMBARAM CHENNAI CENTRAL ASHOK NAGAR MAMBALAM AVADI CHENNAI CENTRAL CHENNAI EGMORE TAMBARAM PERAMBUR ASHOK NAGAR MAMBALAM AVADI
Array ( [response_code] => 200
[stations] => Array (
[0] => Array ( [lat] => 13.0848031Code: Select all
=> MAS [lng] => 80.2749909 [fullname] => CHENNAI CENTRAL [state] => Tamil Nadu ) //i want this first array element only to be printed. [1] => Array ( [lat] => 13.0778142 [code] => MS [lng] => 80.2596615 [fullname] => CHENNAI EGMORE [state] => Tamil Nadu ) [2] => Array ( [lat] => 12.9229153 [code] => TBM [lng] => 80.1274558 [fullname] => TAMBARAM [state] => Tamil Nadu ) [3] => Array ( [lat] => 13.1160178 [code] => PER [lng] => 80.2316646 [fullname] => PERAMBUR [state] => Tamil Nadu ) [4] => Array ( [lat] => 13.0373769 [code] => ASKN [lng] => 80.2122821 [fullname] => ASHOK NAGAR [state] => Tamil Nadu ) [5] => Array ( [lat] => 13.037713 [code] => MBM [lng] => 80.2276873 [fullname] => MAMBALAM [state] => Tamil Nadu ) [6] => Array ( [lat] => 13.1067448 [code] => AVD [lng] => 80.0969511 [fullname] => AVADI [state] => Tamil Nadu ) ) )[/quote]
Re: How to get the specific element value from associative a
Then post your codesathya wrote:Sorry the output i am getting is very wrong
Re: How to get the specific element value from associative a
Json file for get the station fullname by code:
any queries here kindly revert
my php file:{
"response_code": 200,
"stations": [
{
"lng": 83.3731675,
"lat": 26.7605545,
"code": "GKP",
"state": "Uttar Pradesh",
"fullname": "Gorakhpur Jn" <--------i want this fullname only in output.not all the fullnames---->
},
{
"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"
}
]
}
Code: Select all
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Train Status Time Table</TITLE>
<style>
table { width:60%; }
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: white;
width:60%;
}
table#t01 tr:nth-child(odd) { background-color:white; }
table#t01 th {
background-color: blue;
color: white;
}
table#t02 th {
background-color: blue;
color: white;
}
body { background-color: lightblue; }
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
width: auto;
}
li { float: left; }
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) { background-color: #111; }
.active { background-color: #4CAF50; }
</style>
</HEAD>
<BODY>
<CENTER><H2><B>Live Train Running Status</B></H2></CENTER>
<UL>
<LI><A class="active" href="#home">Home</A></LI>
<LI><A href="#news">Live Train Status</A></LI>
<UL style="float:right;list-style-type:none;">
<LI><A href="#about">Blog</A></LI>
<LI><A href="#login">Contact</A></LI>
</UL>
</UL>
<BR />
<CENTER>
<?php
$json = '';
if(isset($_GET['trainnumber']) && (isset($_GET['doj']))) {
$url = 'http://api.railwayapi.com/live/train/'.$_GET['trainnumber'].'/doj/'.$_GET['doj'].'/apikey/*/';
$url_trainname = 'http://api.railwayapi.com/name_number/train/'.$_GET['trainnumber'].'/apikey/*/';
$json = json_decode(file_get_contents($url), true);
$json_trainname = json_decode(file_get_contents($url_trainname), true);
$trainname=$json_trainname ['train']['name'];
$trainnumber = $json['train_number'];
$doj = $json['route'][0]['scharr_date'];
$position = $json['position'];
?>
<TABLE id='t01'>
<TR><TH>Train Number</TH><TH>Date on Journey</TH></TR>
<TR><TD><?= $trainnumber; ?></TD><TD><?= $doj; ?></TD></TR>
<TR><TH>Position</TH></TR>
<TR><TD><?= $trainname.' '.$position; ?></TD></TR>
<TR><TH>Train Name</TH></TR>
<TR><TD><?= $trainname; ?></TD></TR>
</TABLE>
<TABLE>
<TR><TH>Station</TH><TH>Scheduled</TH><TH>Actual</TH><TH>Status/Delay</TH></TR>
<?php
foreach($json['route'] as $stop) {
$url_trainnameconvert = 'http://api.railwayapi.com/code_to_name/code/'.$stop['station'].'/apikey/*/'; //passing the station element from Json Response 1 to Json Response 2
$json_trainnameconvert = json_decode(file_get_contents($url_trainnameconvert), true);
var_dump($url_trainnameconvert);
var_dump($json_trainnameconvert);
$maxname=$json_trainnameconvert['stations']['fullname'];
var_dump($maxname);
foreach($json_trainnameconvert['stations'] as $trainnameconvert) {
($stop['has_arrived'] === true) ? $stop['has_arrived'] = 'Arrived' : $stop['has_arrived'] = 'Still not here yet!';
echo ' <TR><TD>'.$trainnameconvert['fullname'].'</TD><TD>'.$stop['scharr'].'</TD><TD>'.$stop['actarr'].'</TD><TD>'.$stop['has_arrived']."</TD></TR>\n"; //printing the '.$trainnameconvert['fullname'].' using echo to print the train name and station code here.
}
}
} else {
echo 'Something went wrong, please notify to admin ';
}
?>
</TABLE>
</CENTER>
</BODY>
</HTML>
Last edited by requinix on Wed Feb 03, 2016 11:35 pm, edited 1 time in total.
Reason: removing api key by request
Reason: removing api key by request
Re: How to get the specific element value from associative a
You need to target a specific element in the stations array.
should be
Code: Select all
$maxname=$json_trainnameconvert['stations']['fullname'];Code: Select all
$maxname=$json_trainnameconvert['stations'][0]['fullname'];Re: How to get the specific element value from associative a
yes its working fine.you are great Celauran
Re: How to get the specific element value from associative a
but how to include this inside the loop.
Re: How to get the specific element value from associative a
Why do you want it inside the loop if you only want the first item? What are you trying to accomplish?
Re: How to get the specific element value from associative a
actually i want to display the first item only but the remaining elements getting different results.i will analyse and post here.
Re: How to get the specific element value from associative a
got the complete output.thank you so much ..your single question make me think and corrected the code