Welcome to WebmasterWorld Guest from 3.227.3.146

Forum Moderators: coopster & jatar k

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

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

New User

joined:Jan 20, 2016
posts: 5
votes: 0



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?
12:03 pm on Jan 20, 2016 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


$json not $jsont?
12:06 pm on Jan 20, 2016 (gmt 0)

New User

joined:Jan 20, 2016
posts: 5
votes: 0


yes typo mistake.sorry its json only
12:11 pm on Jan 20, 2016 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


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.
12:15 pm on Jan 20, 2016 (gmt 0)

New User

joined:Jan 20, 2016
posts: 5
votes: 0


yes thats an json response ,i have decoded and stored in the $json variable.
12:18 pm on Jan 20, 2016 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


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'; 
12:46 pm on Jan 20, 2016 (gmt 0)

New User

joined:Jan 20, 2016
posts: 5
votes: 0


yes thats not a problem i guess echo ' <TR><TD>'.$station_fullname['fullname']."</TD></TR>\n'; updated .
9:33 pm on Jan 20, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15941
votes: 889


<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.
9:52 pm on Jan 20, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


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'];