2016-04-02 2 views
-1

Ich habe diese GeoJSON ....Abfrage JSON (GeoJSON ...), in PHP zu extrahieren Wert

{ 
    "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" 
} 

... und ich brauche den Wert D6B1-IcUpWc6rEq2v-a4AQ suchen und als ich habe festgestellt, es den Wert des „Schlüssel“ tages Zusammenhang mit der Rückkehr so ​​in diesem Fall gP_RMGgi8Vs26HEtuQBzBw

Jede Anregung/Beispiel für es in PHP zu tun?

Vielen Dank im Voraus !!!

Cesare

Antwort

2

Sie benötigen eine Zeichenfolge und dann foreach entschlüsselten Array json_decode.

Ich schreibe Funktion findKey. Sie müssen zwei Variablen übergeben:

gut zu perfektionieren
<?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)); 
+0

Es funktioniert: Ihre JSON-String und Schlüssel für

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']; } } } 

Dieses Codebeispiel findet die gewünschte Taste gP_RMGgi8Vs26HEtuQBzBw suchen! Danke für die schnelle und vollständige Antwort !!! – Cesare