Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save whaleinvasion/56d34537ede80d854af866fa1fae1046 to your computer and use it in GitHub Desktop.
Save whaleinvasion/56d34537ede80d854af866fa1fae1046 to your computer and use it in GitHub Desktop.
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