PHP/JSON Skipping Over Information? Multiple Test Cases Provided -
i've got json / php question. i'll start off posting brief segment of json, @ least enough convey point:
"results": [ { "members": [ { "side": "majority", "rank": 1, "title": "chairman", "legislator": { "bioguide_id": "t000464", "birthday": "1956-08-21", "chamber": "senate", "contact_form": "http://www.tester.senate.gov/contact/index.cfm", "crp_id": "n00027605", "district": null, "facebook_id": "210573031664", "fax": "202-224-8594", "fec_ids": [ "s6mt00162" ], "first_name": "jon", "gender": "m", "govtrack_id": "412244", "icpsr_id": 40702, "in_office": true, "last_name": "tester", "lis_id": "s314", "middle_name": null, "name_suffix": null, "nickname": null, "office": "706 hart senate office building", "party": "d", "phone": "202-224-2644", "senate_class": 1, "state": "mt", "state_name": "montana", "state_rank": "junior", "term_end": "2019-01-03", "term_start": "2013-01-03", "thomas_id": "01829", "title": "sen", "twitter_id": "testerpress", "votesmart_id": 20928, "website": "http://www.tester.senate.gov", "youtube_id": "senatorjontester" } }, { "side": "majority", "rank": 2, "title": null, "legislator": { "bioguide_id": "p000590", "birthday": "1963-01-10", "chamber": "senate", "contact_form": "http://www.pryor.senate.gov/public/index.cfm?p=contactme", "crp_id": "n00013823", "district": null, "facebook_id": "9248638978", "fax": "202-228-0908", "fec_ids": [ "s0ar00028" ], "first_name": "mark", "gender": "m", "govtrack_id": "300080", "icpsr_id": 40301, "in_office": true, "last_name": "pryor", "lis_id": "s295", "middle_name": null, "name_suffix": null, "nickname": null, "office": "255 dirksen senate office building", "party": "d", "phone": "202-224-2353", "senate_class": 2, "state": "ar", "state_name": "arkansas", "state_rank": "senior", "term_end": "2015-01-03", "term_start": "2009-01-06", "thomas_id": "01701", "title": "sen", "twitter_id": "senmarkpryor", "votesmart_id": 35, "website": "http://www.pryor.senate.gov", "youtube_id": "senatorpryor" } },
alright - information i'm tryin grab title of each legislator, bioguide_id. code i'm using parse information follows:
$url1 = 'http://congress.api.sunlightfoundation.com/committees?fields=members&apikey=xxxxxxxxxxx&per_page=20&page=1'; $response1 = file_get_contents($url1); $key1 = json_decode($response1, true); foreach ($key1['results'] $value){ $title_1 = $value['members'][0]['title']; if($title_1 == null){ $title_1 = "null"; } echo $title_1 . '<br/>' . $value['members'][0]['legislator']['bioguide_id'] . '<br/>'; }
however, results running script follows:
chairman t000464 chairman m001170 chairman b001265 chairman l000261 chairman b000711 chairman k000384 chairman b001267 chairman c001070 chairman w000802 chairman m001176 chairman n000032 chairman k000367 chairman g000555 null l000174 chairman h001069 chairman b001267 chairman d000607 vice chairman b000243 chairman s000148 vice chairman s000148
upon first glance, thought things looked fishy because of amount of chairmen titles floating (when there should several null in between chairmen) , second bioguide_id isn't correct bioguide_id second position (chairman & t000464 looks correct, next 1 shouldn't chairmen m001170, null p000590). i've switched $value['members'][0]... $value['members'][1], , able second address, results weren't correct. there can see allow me grab correct information? how i've worked other json files, seem i'm not doing dramatically incorrect. in advance (i know long).
your foreach
looping through sets of results, not sets of members.
here working foreach
have confirmed works:
foreach ($key1['results'][0]['members'] $value) { $title_1 = $value['title']; if ($title_1 == null) { $title_1 = "null"; } echo $title_1 . '<br/>' . $value['legislator']['bioguide_id'] . '<br/>'; }
Comments
Post a Comment