Return to Snippet

Revision: 63736
at June 3, 2013 10:19 by devowhippit


Initial Code
function break_address( $loc )
{
	$components = explode( ',' , $loc );
	$state_zip = explode( ' ' , trim( $components[2] ) );
	$components_array = array(
		'street' 	=> trim( $components[0] ),
		'city'		=> trim( $components[1] ),
		'state'		=> $state_zip[0],
		'zip'		=> $state_zip[1],
		'country'	=> trim( $components[3] )
	);
	return $components_array;
}

$locations = array();
$location = get_field( 'location_field' );
$location = $location[ 'address' ];
$locations[ 0 ] = break_address( $location );

usort( $locations , function( $a , $b )
{
    return $a[ 'state' ] - $b[ 'state' ];
});

var_dump( $locations );

Initial URL

                                

Initial Description
Breaks up an address (in this case returned by Advanced Custom Fields Location plugin) into it's components. Additionally, sorts a multidimensional array of locations alphabetically by state. This works for basic addresses assuming that address is in the format 'street, city, state, zip, country.' I've written a more extensive version of this snippit that uses Google Geocoding to get the address components and store them in a Wordpress database as meta data.

Initial Title
Turn Over Address Components to an Array

Initial Tags
php, format, google, wordpress

Initial Language
PHP