访问具有大量键的数组

时间:2017-03-09 08:55:36

标签: php arrays json loops foreach

实际上,我正在制作一个带有twitter API的网络应用,以自动化一些信息搜索。我尝试用密钥访问一个数组,但我得不到我想要的东西。

我的目标是在foreach的每个循环上提取screen_name。我为获取推特ID做了类似的事情,但尝试使用此功能并且无法正常工作。

数组如下所示:

array (size=1)
0 => 
array (size=43)
  'id' => float 8.2840004310743E+17
  'id_str' => string '828400043107430400' (length=18)
  'name' => string 'Apuestas LCS' (length=12)
  'screen_name' => string 'ApuestasLCS' (length=11)
  'location' => string 'bet365' (length=6)
  'description' => string 'Grupo GRATUITO* de apuestas sobre LCS (EU/NA/KR), CS:GO, y a veces, NBA, Euroliga, Fútbol y algo de Tenis.
  'entities' => 
    array (size=2)
      'url' => 
        array (size=1)
          ...
      'description' => 
        array (size=1)
          ...
  'protected' => boolean false
  'followers_count' => int 76
  'friends_count' => int 35
  'listed_count' => int 1
  'created_at' => string 'Mon Feb 06 00:28:56 +0000 2017' (length=30)
  'favourites_count' => int 20
  'utc_offset' => int 3600
  'time_zone' => string 'Madrid' (length=6)
  'geo_enabled' => boolean false
  'verified' => boolean false
  'statuses_count' => int 55
  'lang' => string 'es' (length=2)
  'status' => 
    array (size=23)
      'created_at' => string 'Sat Feb 11 17:12:31 +0000 2017' (length=30)
      'id' => float 8.3046454420401E+17
      'id_str' => string '830464544204013568' (length=18)
      'text' => string 'RT @NBA: Last year we got the shot of the year. What will happen this time when the @Warriors visit the @OKCThunder at 8:30pm/et? #NBAonABC…' (length=142)
      'truncated' => boolean false
      'entities' => 
        array (size=4)
          ...
      'source' => string '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>' (length=66)
      'in_reply_to_status_id' => null
      'in_reply_to_status_id_str' => null
      'in_reply_to_user_id' => null
      'in_reply_to_user_id_str' => null
      'in_reply_to_screen_name' => null
      'geo' => null
      'coordinates' => null
      'place' => null
      'contributors' => null
      'retweeted_status' => 
        array (size=23)
          ...
      'is_quote_status' => boolean false
      'retweet_count' => int 2295
      'favorite_count' => int 0
      'favorited' => boolean false
      'retweeted' => boolean false
      'lang' => string 'en' (length=2)
  'contributors_enabled' => boolean false
  'is_translator' => boolean false
  'is_translation_enabled' => boolean false
  'profile_background_color' => string '000000' (length=6)
  'profile_background_image_url' => string 'http://abs.twimg.com/images/themes/theme1/bg.png' (length=48)
  'profile_background_image_url_https' => string 'https://abs.twimg.com/images/themes/theme1/bg.png' (length=49)
  'profile_background_tile' => boolean false
  'profile_image_url' => string 'http://pbs.twimg.com/profile_images/828406532580765696/TZZUKS0X_normal.jpg' (length=74)
  'profile_image_url_https' => string 'https://pbs.twimg.com/profile_images/828406532580765696/TZZUKS0X_normal.jpg' (length=75)
  'profile_banner_url' => string 'https://pbs.twimg.com/profile_banners/828400043107430400/1486388427' (length=67)
  'profile_link_color' => string 'E81C4F' (length=6)
  'profile_sidebar_border_color' => string '000000' (length=6)
  'profile_sidebar_fill_color' => string '000000' (length=6)
  'profile_text_color' => string '000000' (length=6)

所以这里有源代码。

每个人的第一个已经开始工作了。第二个应该

$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?screen_name='.$user.'';
$requestMethod = 'GET';


$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));



 $arrayFriends = json_decode($json, true,512,JSON_BIGINT_AS_STRING);

foreach($arrayFriends['ids'] as $obj){
   echo $obj,",";

$url2 = 'https://api.twitter.com/1.1/users/lookup.json';
$getfield2 = '?user_id='.$obj.'';

$json2 = $twitter->setGetfield($getfield2)
->buildOauth($url2, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));

  echo  $twitter->setGetfield($getfield2)
->buildOauth($url2, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));

  $json3 = json_decode($json2, true);

  //$json3 = var_dump(json_decode($json2, true));

  foreach ($json3[0]['screen_name'] as $nombre) {

      echo $nombre,",";
  }


   /* $sql = "INSERT INTO friendtest (user, friends)
 VALUES ('$user' , '$obj' )";

 if (mysqli_query($conn, $sql)) {
     echo "New record created successfully";
 } else {
     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
 }*/

}

很抱歉这篇帖子太长了。我已经失去了很多时间,需要一些反馈。

0 个答案:

没有答案