Php 如何创建一个将显示json文件中多个元素的条件?
我想创建一个条件来实现这一点: 如果在一个句子中,部门名称等于json文件中的部门名称,我们将显示带有其他元素的句子,并与所选的部门名称链接,例如他的人口、他的部门代码 json文件看起来像这样,比这个长很多:Php 如何创建一个将显示json文件中多个元素的条件?,php,arrays,json,loops,Php,Arrays,Json,Loops,我想创建一个条件来实现这一点: 如果在一个句子中,部门名称等于json文件中的部门名称,我们将显示带有其他元素的句子,并与所选的部门名称链接,例如他的人口、他的部门代码 json文件看起来像这样,比这个长很多: [ { "datasetid": "population-francaise-par-departement-2018", "recordid": "7b81f8adc3cb71b942540e51d868992a7d588595",
[
{
"datasetid": "population-francaise-par-departement-2018",
"recordid": "7b81f8adc3cb71b942540e51d868992a7d588595",
"fields": {
"departement": "Orne",
"code_departement": "61",
"geom": {
"type": "Polygon",
"coordinates": [
[
-0.7399722943,
48.6217032013
],
[
-0.7388681294,
48.622541151
],
[
-0.4329546858,
48.8633021563
],
[
-0.4305570769,
48.8630882975
]
]
},
"geo_point_2d": [
48.62307419424573,
0.127896583868712
],
"population": 282516
},
"geometry": {
"type": "Point",
"coordinates": [
0.127896583868712,
48.62307419424573
]
},
"record_timestamp": "2018-06-21T14:36:09.020+02:00"
},
{
"datasetid": "population-francaise-par-departement-2018",
"recordid": "7b81f8adc3cb71b942540e51d868992a7d588595",
"fields": {
"departement": "Blois",
"code_departement": "28",
"geom": {
"type": "Polygon",
"coordinates": [
[
-0.7399726385,
22.6217542752
],
[
-0.7388681294,
55.622541151
],
[
-0.4329546858,
47.8633021563
],
[
-0.4305570769,
12.8630882975
]
]
},
"geo_point_2d": [
48.62307419424573,
0.127896583868712
],
"population": 254654
},
"geometry": {
"type": "Point",
"coordinates": [
0.127896583868712,
48.62307419424573
]
},
"record_timestamp": "2018-06-21T14:36:09.020+02:00"
}
]
我试过这个:
<?php
$populationdata = file_get_contents('population-francaise-par-departement-2018.json');
$myfile = json_decode($populationdata, true);
foreach($myfile as $record) {
$name = "Blois";
$sentence= 'you are in the department call '. $name . ' the population is about '. $thepopulation .' people ' . "the code departement is: ". $codedepartement;
if($record['fields']['departement'] === $nom){
echo $sentence;
}
}
这里的问题是:
代码显示句子,但代码部门的人口信息不是好的。我如何选择好的
提前谢谢你
每次在我的句子中,部门名称出现在json文件中时,我都希望这样显示:
您所在的部门呼叫人数为254654人,代码部门为:28 您可以尝试这样的函数。它在department字段中查找传递的部门名称,然后使用返回的索引查找人口和代码:
function show_population($data, $department) {
$fields = array_column($data, 'fields');
$k = array_search($department, array_column($fields, 'departement'));
if ($k !== false) {
$sentence = 'you are in the department call '. $department . ' the population is about '. $fields[$k]['population'] .' people ' . "the code departement is: ". $fields[$k]['code_departement'] . PHP_EOL;
}
else {
$sentence = 'the department ' . $department . ' has no data available' . PHP_EOL;
}
return $sentence;
}
echo show_population($myfile, 'Blois');
echo show_population($myfile, 'Paris');
输出:
you are in the department call Blois the population is about 254654 people the code departement is: 28
the department Paris has no data available
在句子$thepopulation=$record['fields']['population']和$CodeDepartment=$record['fields']['code\u Department']之前定义此变量。您也没有定义$nom变量。定义i put$name=Blois这是什么意思?如果$record['fields'['Department']==$nom,请检查您的if条件{这是错误的变量$nom.oh好吧,我之所以这样做是因为我第一次尝试这样的东西$name=$record['fields']['department'];$thepopulation=$record['fields']['population'];$code department=$record['fields']['code_department'];在我的情况下写$name,但他没有工作