1

I have a JSON file containing a list of universities around the world. I want to get only specific universities where a field in the array matches what I need to select. The problem I face is that each university has it's own ID number which makes me unable to figure out how to iterate over the Array. The JSON file can be found on this GitHub repo.

The code that makes me convert the JSON file to an array:

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

    print_r($universityArray);
?>

And a sample of what I get is:

[2942] => Array
        (
            [alpha_two_code] => EG
            [country] => Egypt
            [domain] => aast.edu
            [name] => Arab Academy for 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 is the best or appropriate way to print out only the universities with alpha_two_code == 'EG' or == 'Egypt' for example?

I read the documentation on foreach loop and the examples as well. But still can't get the logic to get what I mentioned above.

Tes3awy
  • 2,166
  • 5
  • 29
  • 51

4 Answers4

2

Check this return only a specific country

<?php
        $json = file_get_contents('university.json');
        $universityArray = json_decode($json, true);
        universities= array()
        for($i=0; $i< count($universityArray); $i++)
        {  
            if($universityArray[$i]["country"] == "Morocco")
             universitises[] = $universityArray[$i];
        }
        var_dump($universitises); 

?>

Youssef Saoubou
  • 591
  • 4
  • 11
2

You can use alpha_two_code as index.

$indexed = [];
foreach($universityArray as $university){
    $index = $university['alpha_two_code'];
    if(!isset($indexed[$index])){
        $indexed[$index] = [];
    }
    $indexed[$index][] = $university;
}

Now you will have universities seperated by alpha_two_code which you can directly access.

print_r($indexed['EG']);

Now as per the best and appropriate part, you may want to cache $indexed. You can create a directory for universities and save JSON encoded $indexed there.

anwerj
  • 2,386
  • 2
  • 17
  • 36
1

you can use the array_filter http://php.net/manual/en/function.array-filter.php function here with a callback. You can then use array_column http://php.net/manual/en/function.array-column.phpto grab just the 'name' column.

$json = file_get_contents('https://github.com/Hipo/university-domains-list/blob/master/world_universities_and_domains.json');
$universityArray = json_decode($json, true);

$filterBy = 'EG';

$newArray = array_filter($universityArray, function ($var) use ($filterBy) {
    return ($var['alpha_two_code'] == $filterBy);
});

print_r($newArray);

$names = array_column($newArray, 'name');

print_r($names);
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
1

You need to read the manual.

Try this:

$names = array();
foreach($universityArray as $u) {
  if($u['alpha_two_code'] == 'EG' || $u['country'] == 'Egypt'){
    $names[] = $u['name'];
  }
}

print_r($names);
catbadger
  • 1,662
  • 2
  • 18
  • 27