【发布时间】:2019-09-02 08:47:23
【问题描述】:
我想创建一个条件来做到这一点: 如果句子中的“部门名称”等于 json 文件中的部门名称,我们会显示带有其他元素的句子,与所选的部门名称相关联,例如他的人口,他的部门代码。
json 文件看起来像这样(比这个长很多):
[
{
"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 文件中:
You are in the department call "Blois" the population is about "254654" people the code department is: "28";
【问题讨论】:
-
在句子
$thepopulation = $record['fields']['population']和$codedepartement = $record['fields']['code_departement']之前定义这个变量 -
你也没有定义
$nom变量。 -
define i put $name ="Blois" 这不好??
-
检查你的if条件
if($record['fields']['departement'] === $nom){这里是错误的变量$nom。 -
哦,好吧,我放这个的原因是我第一次尝试这样的东西 $name=$record['fields']['departement']; $thepopulation=$record['fields']['population']; $codedepartement=$record['fields']['code_departement'];并在我的情况下写 $name 但他没有工作