Forum Moderators: coopster

Message Too Old, No Replies

How to get the specific element value from associative array?

i want to get the fullname field by using the foreach loop

         

livetrainstatus

11:46 am on Jan 20, 2016 (gmt 0)

10+ Year Member




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] => 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 ) ) );



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

foreach($jsont['stations'] as $station_fullname)
{
echo ' <TR><TD>'.$station_fullname['fullname'].'</TD></TR>\n";

}
}
} else {
echo 'Something went wrong, please notify to admin ';
}


can any one help me to get the solution?

vincevincevince

12:03 pm on Jan 20, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$json not $jsont?

livetrainstatus

12:06 pm on Jan 20, 2016 (gmt 0)

10+ Year Member



yes typo mistake.sorry its json only

vincevincevince

12:11 pm on Jan 20, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The way you are defining your Array doesn't look like valid PHP code (it's not quoted/escaped properly for a start);
the loop you've written to read it seems reasonable though.

livetrainstatus

12:15 pm on Jan 20, 2016 (gmt 0)

10+ Year Member



yes thats an json response ,i have decoded and stored in the $json variable.

vincevincevince

12:18 pm on Jan 20, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've not closed the " in your code right?
echo '  <TR><TD>'.$station_fullname['fullname'].'</TD></TR>\n"; 

Should be:
echo '  <TR><TD>'.$station_fullname['fullname'].'</TD></TR>\n'; 

livetrainstatus

12:46 pm on Jan 20, 2016 (gmt 0)

10+ Year Member



yes thats not a problem i guess echo ' <TR><TD>'.$station_fullname['fullname']."</TD></TR>\n'; updated .

lucy24

9:33 pm on Jan 20, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<TR><TD>

You should be ECHOing <tr><td> in lower case to agree with current Best Usage.

This may seem irrelevant, but it points to possible other issues: things that look trivial but can hide substantive problems. It can't hurt to fine-tooth-comb the whole code for potential mismatches among things that are almost-but-not-quite interchangeable (like the single vs. double quotes pointed out earlier).

When posting, copy and paste your actual code. Don't re-type it, or you may accidentally introduce errors that aren't present in the working code. Or, conversely, you may accidentally fix errors that are present in etcetera.

whitespace

9:52 pm on Jan 20, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



i want to get the first element in the array as [fullname] => CHENNAI CENTRAL only with forach loop


Why do you want to use a for[e]ach() loop?

If you only want the first element of the array then you shouldn't be using a foreach() loop? If you do then you'll get "all the full names in the above array" - which appears to be what is happening. At the very least you could break after the first iteration. But you are better off just going for the first element, for example:


$firstStation = reset($json['stations']);
$firstFullname = $firstStation['fullname'];


If you are on PHP 5.4+ (which introduces function array dereferencing) then you can do the above in one statement:


$firstFullname = reset($json['stations'])['fullname'];