0

Working on a flight booking website. Have a json file which contains the airport codes, city name and country name.

[{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}]

Now if my iata airport code matches to UTK i want to return the name.

kapa
  • 77,694
  • 21
  • 158
  • 175
Radhakrishna Rayidi
  • 510
  • 2
  • 9
  • 24
  • 1
    Use the for loop to iterate over the array and check for the code. – msapkal Feb 19 '14 at 10:00
  • Please do not add a PHP tag. Your tag is invalid as the question does not relate to PHP. Your question is now a bad question because it is not clear what you are asking. Do you want a solution in javascript or PHP? You cannot have both - unless your specific requirement uses both technologies... in which case you need to show your current code attempts. I will have to downvote this question until you improve it, sorry – musefan Feb 19 '14 at 10:26
  • Here PHP and json have relation know, If some body searching for the same code in php, This answers might help them. So i added the tag – Radhakrishna Rayidi Feb 19 '14 at 10:28
  • @RadhakrishnaRayidi: SO doesn't work like that! You cannot ask generic questions asking for solutions in multiple languages. Pick one or the other – musefan Feb 19 '14 at 10:29

3 Answers3

1

Use filter() to find the object within the Array by its iata:

var arr = [{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}];

function findByCode(arr, iata){
    var filtered = arr.filter(function(e){return e.iata = iata});
    return (filtered.length == 1) ? filtered[0]:undefined;
}

console.log(findByCode(arr,"UTK").name);

JS Fiddle: http://jsfiddle.net/dE9nP/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

This is basic searching! You need to loop over the array, and check each item for the matching code, then return the result. Here is a function:

function findAirport(myArray, iata){
    for(var i = 0; i < myArray.length; i++){
        var item = myArray[i];
        if(item["iata"] === iata){
            return item;
        }
    }
    return null;
}

You can use that function like so:

var airports = [{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}];
var match = findAirport(airports, "UTK");
if(match){
    console.log("name = " + match.name);
}

Just to highlight my preference to using simple for loops over other functions such as filter, here is a performance test comparing my answer to the other one that uses filter. I know it's meaningless in most real use cases, but I wanted to keep a reference to the jsperf link :)

NOTE: This answer was provided before the question tags were edited. At the time of posting this question was asking for a javascript solution

musefan
  • 47,875
  • 21
  • 135
  • 185
0
$string = '[{"iata":"UTK","lon":"169.86667","iso":"MH","status":1,"name":"Utirik Airport","continent":"OC","type":"airport","lat":"11.233333","size":"small"}]';
 $data = json_decode($string);
echo count($data);
for($i=0;$i<count($data);$i++){
if($data[$i]->iata == 'UTK') echo $data[$i]->name;
}

you can use file_get_contents if the data in a file instead of $string;

JohnTaa
  • 2,722
  • 2
  • 15
  • 15
  • This is not a PHP question. Javascript only answers please – musefan Feb 19 '14 at 10:19
  • Even thow, The answer helps for PHP Developers also. Thats y +1 – Radhakrishna Rayidi Feb 19 '14 at 10:21
  • @RadhakrishnaRayidi: This isn't how stackoverflow works. Answers should answer the exact question. Not be a reference point for alternate language implementations. There are already loads of questions (with answers) about how to search a JSON array in PHP – musefan Feb 19 '14 at 10:23
  • I think the question is tagged as (javascript,php) question, So one of the two solutions is enough. – JohnTaa Feb 19 '14 at 10:27
  • @JohnTaa: It wasn't to start with... and now it is incorrectly tagged. Do you think it would be acceptable to add other languages too? Shall we add a C# answer? maybe a Java one too? – musefan Feb 19 '14 at 10:30