Last active
May 17, 2018 12:03
-
-
Save whaleinvasion/56d34537ede80d854af866fa1fae1046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_yandex_human_address( $geo_object ) { | |
$geo_object = json_decode( $geo_object, true ); | |
$geo_object = $geo_object['response']; | |
$geo_object = $geo_object['GeoObjectCollection']['featureMember'][0]['GeoObject']; | |
$declaration = array( | |
'CountryName' => 'country', | |
'AdministrativeAreaName' => 'area', | |
'SubAdministrativeAreaName' => 'subarea', | |
'LocalityName' => 'city', | |
'DependentLocalityName' => 'subcity', | |
'ThoroughfareName' => 'street', | |
'PremiseNumber' => 'house' | |
); | |
$parseParams = array( | |
'Country', | |
'CountryName', | |
'AdministrativeArea', | |
'AdministrativeAreaName', | |
'Locality', | |
'LocalityName', | |
'Thoroughfare', | |
'ThoroughfareName', | |
'Premise', | |
'PremiseNumber', | |
'DependentLocality', | |
'DependentLocalityName', | |
'SubAdministrativeArea', | |
'SubAdministrativeAreaName' | |
); | |
$level = $geo_object['metaDataProperty']['GeocoderMetaData']['AddressDetails']; | |
$result = array(); | |
$nextLevel = 0; | |
while ( 1 ) { | |
$goDeeper = false; | |
foreach ( $level as $key => $val ) { | |
if ( in_array( $key, $parseParams ) ) { | |
if ( is_array( $level[ $key ] ) ) { | |
$nextLevel = $level[ $key ]; | |
$goDeeper = true; | |
} else { | |
foreach ( $declaration as $declKey => $declVal ) { | |
if ( $declKey == $key ) { | |
$result[ $declaration[ $key ] ] = $level[ $key ]; | |
} | |
} | |
} | |
} | |
} | |
if ( $goDeeper ) { | |
$level = $nextLevel; //we need to go deeper ;) | |
} else { | |
break; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment