【问题标题】:Query JSON (GeoJSON ...), in PHP to extract value查询 JSON (GeoJSON ...),在 PHP 中提取值
【发布时间】:2016-04-02 16:48:44
【问题描述】:

我有这个 GeoJSON ....

{
    "more": true,
    "features": [{
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [7.672117, 44.901697],
                [7.672137, 44.901766],
                [7.672167, 44.901828],
                [7.672207, 44.901888]
            ],
            "bbox": [7.671508, 44.901697, 7.676434, 44.912198]
        },
        "properties": {
            "boundary": {
                "min_lat": 44.901697,
                "min_lon": 7.671508,
                "max_lat": 44.912198,
                "max_lon": 7.676434
            },
            "captured_at": 1432989312291,
            "key": "Lm7zCv3niXy9jBDmaKEuzw",
            "keys": ["O-UdnDpmS8_WTQgOqkj8_w", "BQUybUzc2liYTx5Rc6lEEA", "-n5Yw2WEgnLTVk4kVJbnGQ"]
        }
    }, {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [7.67173, 44.912223],
                [7.671718, 44.912186],
                [7.671685, 44.912138],
                [7.671668, 44.912084]
            ],
            "bbox": [7.671508, 44.911598, 7.67173, 44.912223]
        },
        "properties": {
            "boundary": {
                "min_lat": 44.911598,
                "min_lon": 7.671508,
                "max_lat": 44.912223,
                "max_lon": 7.67173
            },
            "captured_at": 1432985665206,
            "key": "gP_RMGgi8Vs26HEtuQBzBw",
            "keys": ["0PZ_0b5gDwgv_wGR-PaH6g", "D6B1-IcUpWc6rEq2v-a4AQ", "L9uEPoXiSjagWY2hPTRpBg"]
        }
    }, {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [7.671187, 44.911639],
                [7.671243, 44.911675],
                [7.671249, 44.911742],
                [7.671262, 44.911796]
            ],
            "bbox": [7.671137, 44.911639, 7.671674, 44.912609]
        },
        "properties": {
            "boundary": {
                "min_lat": 44.911639,
                "min_lon": 7.671137,
                "max_lat": 44.912609,
                "max_lon": 7.671674
            },
            "captured_at": 1433505642167,
            "key": "wGJ8pn9A41vdyQN-WSIT_Q",
            "keys": ["DVZQEFI_8qczLI99NCpDkQ", "edPjE41cA8h4HIzmbS0MyA", "JYghFOvUuPPtpQL5ff0lmQ"]
        }
    }, {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [7.671765, 44.912323],
                [7.671756, 44.912292],
                [7.671746, 44.912258],
                [7.671734, 44.912223]
            ],
            "bbox": [7.671481, 44.911138, 7.672658, 44.912323]
        },
        "properties": {
            "boundary": {
                "min_lat": 44.911138,
                "min_lon": 7.671481,
                "max_lat": 44.912323,
                "max_lon": 7.672658
            },
            "captured_at": 1432743361672,
            "key": "4x5ay3CHwgxFTdIIQg81-A",
            "keys": ["lx30DGH6cFpa5VWD98pDDA", "xvJ2X2FeFfCDm2cVvPgS6A", "gBJFHuS19-2k9fs3_vJ8zQ"]
        }
    }],
    "type": "FeatureCollection"
}

... 我需要搜索值 D6B1-IcUpWc6rEq2v-a4AQ 并且当我找到它时返回相关的“key”标签的值,所以在这种情况下 gP_RMGgi8Vs26HEtuQBzBw

有什么建议/例子可以用 PHP 做吗?

提前非常感谢您!!!

切萨雷

【问题讨论】:

    标签: php json parsing geojson


    【解决方案1】:

    你需要json_decode你的字符串,然后foreach解码数组。

    我写函数findKey。您需要传递两个变量:您的 json 字符串和要搜索的键

    function findKey($geoJson, $key) {
        $geoArray = json_decode($geoJson, true);
        foreach ($geoArray['features'] as $geoFeature) {
            if (in_array($key, $geoFeature['properties']['keys'])) {
                return $geoFeature['properties']['key'];
                }
        }
    }
    

    此代码示例找到您想要的密钥gP_RMGgi8Vs26HEtuQBzBw

    <?php
    
    $geoJson = <<<EOF
    {
        "more": true,
        "features": [{
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [7.672117, 44.901697],
                    [7.672137, 44.901766],
                    [7.672167, 44.901828],
                    [7.672207, 44.901888]
                ],
                "bbox": [7.671508, 44.901697, 7.676434, 44.912198]
            },
            "properties": {
                "boundary": {
                    "min_lat": 44.901697,
                    "min_lon": 7.671508,
                    "max_lat": 44.912198,
                    "max_lon": 7.676434
                },
                "captured_at": 1432989312291,
                "key": "Lm7zCv3niXy9jBDmaKEuzw",
                "keys": ["O-UdnDpmS8_WTQgOqkj8_w", "BQUybUzc2liYTx5Rc6lEEA", "-n5Yw2WEgnLTVk4kVJbnGQ"]
            }
        }, {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [7.67173, 44.912223],
                    [7.671718, 44.912186],
                    [7.671685, 44.912138],
                    [7.671668, 44.912084]
                ],
                "bbox": [7.671508, 44.911598, 7.67173, 44.912223]
            },
            "properties": {
                "boundary": {
                    "min_lat": 44.911598,
                    "min_lon": 7.671508,
                    "max_lat": 44.912223,
                    "max_lon": 7.67173
                },
                "captured_at": 1432985665206,
                "key": "gP_RMGgi8Vs26HEtuQBzBw",
                "keys": ["0PZ_0b5gDwgv_wGR-PaH6g", "D6B1-IcUpWc6rEq2v-a4AQ", "L9uEPoXiSjagWY2hPTRpBg"]
            }
        }, {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [7.671187, 44.911639],
                    [7.671243, 44.911675],
                    [7.671249, 44.911742],
                    [7.671262, 44.911796]
                ],
                "bbox": [7.671137, 44.911639, 7.671674, 44.912609]
            },
            "properties": {
                "boundary": {
                    "min_lat": 44.911639,
                    "min_lon": 7.671137,
                    "max_lat": 44.912609,
                    "max_lon": 7.671674
                },
                "captured_at": 1433505642167,
                "key": "wGJ8pn9A41vdyQN-WSIT_Q",
                "keys": ["DVZQEFI_8qczLI99NCpDkQ", "edPjE41cA8h4HIzmbS0MyA", "JYghFOvUuPPtpQL5ff0lmQ"]
            }
        }, {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [7.671765, 44.912323],
                    [7.671756, 44.912292],
                    [7.671746, 44.912258],
                    [7.671734, 44.912223]
                ],
                "bbox": [7.671481, 44.911138, 7.672658, 44.912323]
            },
            "properties": {
                "boundary": {
                    "min_lat": 44.911138,
                    "min_lon": 7.671481,
                    "max_lat": 44.912323,
                    "max_lon": 7.672658
                },
                "captured_at": 1432743361672,
                "key": "4x5ay3CHwgxFTdIIQg81-A",
                "keys": ["lx30DGH6cFpa5VWD98pDDA", "xvJ2X2FeFfCDm2cVvPgS6A", "gBJFHuS19-2k9fs3_vJ8zQ"]
            }
        }],
        "type": "FeatureCollection"
    }
    EOF;
    
    $key = 'D6B1-IcUpWc6rEq2v-a4AQ';
    
    function findKey($geoJson, $key) {
        $geoArray = json_decode($geoJson, true);
        foreach ($geoArray['features'] as $geoFeature) {
            if (in_array($key, $geoFeature['properties']['keys'])) {
                return $geoFeature['properties']['key'];
                }
        }
    }
    
    var_dump(findKey($geoJson,$key));
    

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多