从twitter api Yahoo WOEID按国家/地区显示位置

时间:2015-10-13 19:49:33

标签: php arrays json twitter-oauth

下面是示例标记和json,我想要实现的是使用标记列出json countrywise中可用的所有位置。我可以轻松列出所有位置,但很难找到一种方法来按国家列出它们。

<select>
      <optgroup value="23424977" label="United States">
        <option value="2352824">Albuquerque</option>
        <option value="2357024">Atlanta</option>
        <option >...all locations under united states...</option>

      </optgroup>
      <optgroup value="23424900" label="Mexico">
        <option value="110978">Acapulco</option>
        <option value="115958">Chihuahua</option>
      </optgroup>
    </select> 

/ #################以下是一个样本JSON ##################### ### /

    Array(
/**********COUTNRY United States*************/

    [335] => Array
        (
            [name] => Albuquerque
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/2352824
            [parentid] => 23424977
            [country] => United States
            [woeid] => 2352824
            [countryCode] => US
        )

    [336] => Array
        (
            [name] => Atlanta
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/2357024
            [parentid] => 23424977
            [country] => United States
            [woeid] => 2357024
            [countryCode] => US
        )

    [337] => Array
        (
            [name] => Austin
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/2357536
            [parentid] => 23424977
            [country] => United States
            [woeid] => 2357536
            [countryCode] => US
        )

    [338] => Array
        (
            [name] => Baltimore
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/2358820
            [parentid] => 23424977
            [country] => United States
            [woeid] => 2358820
            [countryCode] => US
        )

     [460] => Array
        (
            [name] => United States
            [placeType] => Array
                (
                    [code] => 12
                    [name] => Country
                )

            [url] => http://where.yahooapis.com/v1/place/23424977
            [parentid] => 1
            [country] => United States
            [woeid] => 23424977
            [countryCode] => US
        )


/**********COUTNRY MEXICO*************/
   [435] => Array
        (
            [name] => Mexico
            [placeType] => Array
                (
                    [code] => 12
                    [name] => Country
                )

            [url] => http://where.yahooapis.com/v1/place/23424900
            [parentid] => 1
            [country] => Mexico
            [woeid] => 23424900
            [countryCode] => MX
        )
    [37] => Array
        (
            [name] => Acapulco
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/110978
            [parentid] => 23424900
            [country] => Mexico
            [woeid] => 110978
            [countryCode] => MX
        )
    [39] => Array
        (
            [name] => Chihuahua
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/115958
            [parentid] => 23424900
            [country] => Mexico
            [woeid] => 115958
            [countryCode] => MX
        )
    [40] => Array
        (
            [name] => Mexico City
            [placeType] => Array
                (
                    [code] => 7
                    [name] => Town
                )

            [url] => http://where.yahooapis.com/v1/place/116545
            [parentid] => 23424900
            [country] => Mexico
            [woeid] => 116545
            [countryCode] => MX
        )
    )

我使用以下代码生成一个下拉菜单,其中列出了所有位置,但我正在寻找的是让它更有条理,以便属于同一个国家/地区的所有位置都属于它

   <?php

require_once("twitteroauth.php"); //Path to twitteroauth library

$consumerkey = "*******SECRET************";
$consumersecret = "*******SECRET************";
$accesstoken = "*******SECRET************";
$accesstokensecret = "*******SECRET************";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}


$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/trends/available.json?id=1");


$jdcode = json_encode($tweets);
$loc_namearray = (json_decode($jdcode, true));

$loc_name = array();
$loc_id = array();
foreach ($loc_namearray as $value) {

    array_push($loc_name, $value['name']);
    array_push($loc_id, $value['woeid']);
}
$locations = array_combine($loc_name, $loc_id);

?>


    <select class="locations" id="location" name="location">
        <option>Select Location</option>
        <?php 
         foreach ($locations as $value => $key) { 
         ?>

         <option value="<?php echo $key ?>"><?php echo $value ?></option>
            <?php
         }
         ?>
        ?>
    </select>

0 个答案:

没有答案