Page 1 of 1

How to get the specific element value from associative array

Posted: Wed Jan 20, 2016 5:51 am
by sathya
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.
$json=Array ( [response_code] => 200
[stations] => Array (
[0] => Array ( [lat] => 13.0848031

Code: 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?

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 6:10 am
by sathya
$jsont is typo ,actually its $json

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 6:27 am
by Celauran

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

Posted: Wed Jan 20, 2016 9:47 am
by sathya
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'];

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 10:01 am
by Celauran
Typo. The array key is fullname, not name. Apologies.

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 10:24 am
by sathya
Celauran wrote:Typo. The array key is fullname, not name. Apologies.
Sorry the output i am getting is very wrong.also its printing all the stations
PALANI 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
i have many associative arrays i want to get only the first element in loop [fullname] => CHENNAI CENTRAL like that for the below associative array
Array ( [response_code] => 200
[stations] => Array (
[0] => Array ( [lat] => 13.0848031

Code: 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

Posted: Wed Jan 20, 2016 10:49 am
by Celauran
sathya wrote:Sorry the output i am getting is very wrong
Then post your code

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 11:00 am
by sathya
Json file for get the station fullname by code:
{
"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"
}
]
}
my php file:

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>
any queries here kindly revert

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 11:11 am
by Celauran
You need to target a specific element in the stations array.

Code: Select all

$maxname=$json_trainnameconvert['stations']['fullname'];
should be

Code: Select all

$maxname=$json_trainnameconvert['stations'][0]['fullname'];

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 11:18 am
by sathya
yes its working fine.you are great Celauran

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 11:27 am
by sathya
but how to include this inside the loop.

Re: How to get the specific element value from associative a

Posted: Wed Jan 20, 2016 11:29 am
by Celauran
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

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

Posted: Wed Jan 20, 2016 11:42 am
by sathya
got the complete output.thank you so much ..your single question make me think and corrected the code