【问题标题】:how can i create a condition who will display several element from a json file?如何创建一个条件来显示 json 文件中的多个元素?
【发布时间】: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 但他没有工作

标签: php arrays json loops


【解决方案1】:

你可以试试这样的功能。它在departement字段中查找传递的部门名称,然后使用返回的索引查找人口和代码:

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

Demo on 3v4l.org

【讨论】:

    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2019-06-12
    • 2020-01-28
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多