用PHP查询JSON(GeoJSON…)以提取值
我有这个GeoJSON用PHP查询JSON(GeoJSON…)以提取值,php,json,parsing,geojson,Php,Json,Parsing,Geojson,我有这个GeoJSON { "more": true, "features": [{ "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [7.672117, 44.901697], [7.672137, 44.901766],
{
"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,当我找到它时,返回键标记相关的值,所以在本例中是gP_RMGgi8Vs26HEtuQBzBw
有什么用PHP实现的建议/示例吗
提前非常感谢
Cesare您需要对字符串进行json_解码,然后对每个解码的数组进行解码
我写函数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));