У меня есть этот 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 и, когда я нашел его, вернуть значение тега «ключ», так что в этом случае gP_RMGgi8Vs26HEtuQBzBw
Любое предложение / пример, чтобы сделать это на PHP?
Заранее большое спасибо!!!
Чезаре
Вам нужно 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));
Других решений пока нет …