arrays - Iterate over JSON file using PHP -


this question has answer here:

i have json file containing list of universities around world. want specific universities field in array matches need select. problem face each university has it's own id number makes me unable figure out how iterate on array. json file can found on github repo.

the code makes me convert json file array:

<?php     $json = file_get_contents('universities_list.json');     $universityarray = json_decode($json, true);      print_r($universityarray); ?> 

and sample of is:

[2942] => array         (             [alpha_two_code] => eg             [country] => egypt             [domain] => aast.edu             [name] => arab academy science & technology             [web_page] => http://www.aast.edu/         )      [2943] => array         (             [alpha_two_code] => eg             [country] => egypt             [domain] => akhbaracademy.edu.eg             [name] => akhbar el yom academy             [web_page] => http://www.akhbaracademy.edu.eg/         ) 

what best or appropriate way print out universities alpha_two_code == 'eg' or == 'egypt' example?

i read documentation on foreach loop , examples well. still can't logic mentioned above.

you need read manual.

try this:

$names = array(); foreach($universityarray $u) {   if($u['alpha_two_code'] == 'eg' || $u['country'] == 'egypt'){     $names[] = $u['name'];   } }  print_r($names); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -