-2

如何使用 PHP 和 foreach 循环解析这个 FB 帐户信息?我正在使用此代码来获取用户页面:

$user_profile = $facebook->api('/me/accounts');

print_r($user_profile["data"]); 

这是生成的数组,我试图获取名称和 ID:

Array
( 
  [0] => Array
  ( 
    [name] => RodBull 
    [access_token] => XXXXXXX
    [category] => Company 
    [id] => 101332379991045 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] => MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [1] => Array
  ( 
    [name] => Jobyer 
    [access_token] => XXXXXX
    [category] => Cause 
    [id] => 177780562324865 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] => MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [2] => Array
  ( 
    [name] => Testing it 
    [access_token] => XXXXXXX 
    [category] => Education 
    [id] => 338949122821475 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] =>` MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [3] => Array
  ( 
    [name] => Wellness Incentives Plus 
    [access_token] => XXXXXX
    [category] => Consulting/business services 
    [id] => 280634342048932 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] => MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [4] => Array
  ( 
    [name] => Cashierr 
    [access_token] => XXXXXX
    [category] => Community organization 
    [id] => 131516906950255 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] => MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [5] => Array
  ( 
    [name] => 100x35.me 
    [access_token] => XXXXXX
    [category] => Community organization 
    [id] => 297550586926613 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] => MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [6] => Array
  ( 
    [name] => Faaafavdsfsd 
    [access_token] => XXXXXX
    [category] => Cause 
    [id] => 336675629760824 
    [perms] => Array ( 
    [0] => ADMINISTER 
    [1] => EDIT_PROFILE 
    [2] => CREATE_CONTENT 
    [3] => MODERATE_CONTENT 
    [4] => CREATE_ADS 
    [5] => BASIC_ADMIN )
  ) 
  [7] => Array
  ( 
    [name] => The Page App Tab by Santu 
    [access_token] => XXXXXXX
    [category] => Application 
    [id] => 402022163198499
  ) 
  [8] => Array
  ( 
    [name] => Button blog 
    [access_token] => XXXXXXXX
    [category] => Application 
    [id] => 194345950699984
  ) 
  [9] => Array
  ( 
    [name] => Twit 
    [access_token] => XXXXXX
    [category] => Application 
    [id] => 473412539337463
  ) 
  [10] => Array
  ( 
    [name] => Invite Friends 
    [access_token] => XXXXXX
    [category] => Application 
    [id] => 112138232257401
  ) 
  [11] => Array
  ( 
    [name] => Candygirl19 
    [access_token] => XXXXXXX
    [category] => Application 
    [id] => 458913407468223
  ) 
  [12] => Array
  ( 
    [name] => Dokter Winkel 
    [access_token] => XXXXXXX
    [category] => Application 
    [id] => 214744621969104
  ) 
  [13] => Array
  ( 
    [name] => Testin James 
    [access_token] => XXXXXXX
    [category] => Application 
    [id] => 186137041506535
  ) 
  [14] => Array
  ( 
    [name] => Fans Avenue 
    [access_token] => XXXXXX
    [category] => Application 
    [id] => 129009763896253
  ) 
  [15] => Array
  ( 
    [name] => Automotive 
    [access_token] => XXXXXXX
    [category] => Application 
    [id] => 304776169593014
  ) 
  [16] => Array
  ( 
    [name] => Dealers 
    [access_token] => XXXXXX
    [category] => Application 
    [id] => 267196013320187
  ) 
  [17] => Array
  ( 
    [name] => Empleo 
    [access_token] => XXXXXX
    [category] => Application 
    [id] => 110682139041185
  ) 
  [18] => Array
  ( 
    [name] => Free Account 
    [access_token] => XXXXXXXX
    [category] => Application 
    [id] => 227897480601648
  ) 
  [19] => Array
  ( 
    [name] => Bienes Raices 
    [access_token] => XXXXXXX
    [category] => Application 
    [id] => 255317971171412
  ) 
  [20] => Array
  ( 
    [name] => Autos 
    [access_token] => XXXXXX
    [category] => Application 
    [id] => 276616169025835
  )
)
4

2 回答 2

0
<?php
  $user_profile = $facebook->api('/me/accounts');
  foreach ($user_profile["data"] as $profile){
    $name = $profile["name"];
    $id = $profile["id"];
    echo "Name is $name and id is $id <br />";
  }
?>
于 2012-10-03T22:23:01.937 回答
0
 $names = array();
 $ids = array();
 foreach ($data as $d) {
   $names[] = $d['name'];
   $ids[] = $d['id'];
 }
于 2012-10-03T22:23:38.037 回答