将多维数组拆分为可重用变量?

时间:2014-01-09 14:50:19

标签: php wordpress loops google-maps-api-3 multidimensional-array

您好我目前在WordPress中GeoCoding地址列表,但我的代码不是特定于WordPress的。我正在尝试从Google获取数组

Array ( 
                    [Response] => Array (
                        [Status] => OK 
                        [Request] => street_address
                     )
                    [Geometry] => Array ( 
                        [Latitude] => 37.564096 
                        [Longitude] => -97.2657311
                 ) 
      )

将其拆分为循环外的可重用变量;但我被卡住..我的代码返回

(-97.469014, 2037.77663)(-97.2657311, 2037.564096)

这是GeoCoded所有地址的列表(目前只有2个),但我需要做的是将每一对放入变量中,以便我可以在任何地方重复使用它们。

这是我的代码:

<?php   function geoCoords() { 

        require('cbd/GoogleGeocode.php');

        $apiKey = 'REMOVEDFORPRIVACYPURPOSES';
        $geo = new GoogleGeocode( $apiKey );

        // WP_User_Query arguments
        $args = array (
            'count_total'    => true,
            'fields'         => 'all_with_meta',
        );

        // The User Query
        $user_query = new WP_User_Query( $args );

        // The User Loop
        if ( ! empty( $user_query->results ) ) {
            foreach ( $user_query->results as $user ) {
                $userLocations = unserialize($user->userLocations);
                if ($userLocations) foreach($userLocations as $userLocation) { 
                    $userFinalLocation = $userLocation['Address'] . ' ' .  $userLocation['City, State'] . ' ' .  $userLocation['Zip'];
                    $userLocationCoords = $geo->geocode($userFinalLocation);
                    echo '(' . $userLocationCoords['Geometry']['Longitude'] . ', ' . $userLocationCoords['Geometry']['Latitude'] . ')';
                }
}}} ?>

0 个答案:

没有答案