2016-04-02 28 views
-1

我有这个以GeoJSON ....查询JSON(GeoJSON的...),在PHP中提取价值

{ 
    "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返回相关的“钥匙”变量的值,这样的吗?

非常感谢您提前!

切萨雷

回答

2

您需要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)); 
+0

它的工作完善好!感谢您的快速和完整的回应! – Cesare