使用PHP迭代JSON文件
我有一个JSON文件,其中包含世界各地大学的列表。我只想得到数组中某个字段与我需要选择的字段匹配的特定大学。我面临的问题是,每所大学都有自己的ID号,这使我无法理解如何迭代数组。可以在此repo上找到JSON文件 使我将JSON文件转换为数组的代码:使用PHP迭代JSON文件,php,arrays,json,for-loop,foreach,Php,Arrays,Json,For Loop,Foreach,我有一个JSON文件,其中包含世界各地大学的列表。我只想得到数组中某个字段与我需要选择的字段匹配的特定大学。我面临的问题是,每所大学都有自己的ID号,这使我无法理解如何迭代数组。可以在此repo上找到JSON文件 使我将JSON文件转换为数组的代码: <?php $json = file_get_contents('universities_list.json'); $universityArray = json_decode($json, true); prin
<?php
$json = file_get_contents('universities_list.json');
$universityArray = json_decode($json, true);
print_r($universityArray);
?>
什么是最好的或适当的方式,仅打印出带有alpha\u two\u code=='EG'
或=='Egypt'
的大学
我阅读了有关的文档和示例。但是仍然无法获取逻辑来获取我上面提到的内容。您可以在这里使用带有回调的
array\u filter
函数。然后,您可以使用array\u column
仅获取“name”列
$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);
您可以在这里使用带有回调的
array\u filter
函数。然后,您可以使用array\u column
仅获取“name”列
$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);
你需要阅读手册 试试这个:
$names = array();
foreach($universityArray as $u) {
if($u['alpha_two_code'] == 'EG' || $u['country'] == 'Egypt'){
$names[] = $u['name'];
}
}
print_r($names);
你需要阅读手册 试试这个:
$names = array();
foreach($universityArray as $u) {
if($u['alpha_two_code'] == 'EG' || $u['country'] == 'Egypt'){
$names[] = $u['name'];
}
}
print_r($names);
仅在特定国家/地区检查此申报表
<?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);
仅在特定国家/地区检查此申报表
<?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);
您可以使用alpha\u two\u code
作为索引
$indexed = [];
foreach($universityArray as $university){
$index = $university['alpha_two_code'];
if(!isset($indexed[$index])){
$indexed[$index] = [];
}
$indexed[$index][] = $university;
}
现在,您将拥有由alpha\u two\u code
分隔的大学,您可以直接访问这些大学
print_r($indexed['EG']);
现在,根据最佳和适当的部分,您可能需要缓存$index
。您可以为大学创建一个目录,并在那里保存JSON编码的$index
您可以使用alpha\u two\u code
作为索引
$indexed = [];
foreach($universityArray as $university){
$index = $university['alpha_two_code'];
if(!isset($indexed[$index])){
$indexed[$index] = [];
}
$indexed[$index][] = $university;
}
现在,您将拥有由alpha\u two\u code
分隔的大学,您可以直接访问这些大学
print_r($indexed['EG']);
现在,根据最佳和适当的部分,您可能需要缓存$index
。您可以为大学创建一个目录,并在那里保存JSON编码的$index
我怎样才能只打印大学的名称?@Tes3awy我已经为你更新了我的示例我怎样才能只打印大学的名称?@Tes3awy我已经为你更新了我的示例你的代码只打印一所大学它现在已经修复了。现在试试。你的代码只打印一所大学。现在已经修好了。现在试试。