2015-06-08 200 views
2

我想從API中獲取一些數據,並將其放入數組,然後將其放入MySQL中。PHP數組foreach

我的代碼:

$find_sql = "SELECT * FROM `scrape`"; 
$users_to_scrape = $app['db']->fetchAll($find_sql); 

$instagram = $app['instagram']; 

$oauth = json_decode(file_get_contents($app['oauth_path'])); 

$instagram->setAccessToken($oauth); 

foreach($users_to_scrape as $user_to_scrape) { 
    printf("Getting info for %s <%s>\n", $user_to_scrape['instagram_id'], $user_to_scrape['user_name']); 
    $follows = $instagram->getUser($user_to_scrape['instagram_id'], 999); 

     foreach($follows->data as $follow) { 
      echo var_dump($follows); 
      $data = array(
       'instagram_id' => $follow->id, 
       'followed_by_instgram_id' => $user_to_scrape['instagram_id'], 
       'user_name' => $follow->username, 
       'full_name' => iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($follow->full_name)), 
       'profile_picture' => $follow->profile_picture, 
       'followers' => $follow->counts->followed_by, 
       'follows' => $follow->counts->follows 
      ); 
      printf("+ %s <%s>\n", $data['instagram_id'], $data['user_name']); 
       //skapa tabell med follows (instagram_id, 

      if ($follow->counts->followed_by >= "30000") { 
       $app['db']->insert('follows', $data); 
      } 
     } 
    } 

的Vardump返回此:

object(stdClass)#111 (2) { 
    ["meta"]=> 
    object(stdClass)#112 (1) { 
    ["code"]=> 
    int(200) 
    } 
    ["data"]=> 
    object(stdClass)#113 (7) { 
    ["username"]=> 
    string(9) "Dimbos" 
    ["bio"]=> 
    string(97) "•Have fun in life Contact: [email protected]" 
    ["website"]=> 
    string(24) "http://www.life.com" 
    ["profile_picture"]=> 
    string(106) "https://xxertt.com/hphotos-ak-xaf1/t51.2885-19/11311351_362556250614181_543_a.jpg" 
    ["full_name"]=> 
    string(10) "Dimbo" 
    ["counts"]=> 
    object(stdClass)#114 (3) { 
     ["media"]=> 
     int(113) 
     ["followed_by"]=> 
     int(256673) 
     ["follows"]=> 
     int(345) 
    } 
    ["id"]=> 
    string(8) "38353560" 
    } 
} 

我收到的錯誤是這樣的:

注意:試圖讓非財產 - 對象/var/www/script.php在線

第40行,我們有這樣的: 'instagram_id' => $follow->id, 我還對下面的一組陣列得到錯誤。

難以想象。

+0

根據'var_dump'看起來'$ follow'只是'follow'的一個實例,而不是'foreach'所期望的'follow'的數組。 – chiliNUT

回答

3

由於$follows->datastdClass對象,具有foreach將循環在每個其屬性的迭代它單獨地,返回每個屬性的值。這意味着雖然id存在於循環中,但它僅僅是循環的最後一個數據元素,它的屬性名稱不可訪問。

使用foreach$follow直接產生價值,而不是性能,迭代器值,如下所示:

// Value of $follow on each loop iteration: 
"Dimbos" 
"•Have fun in life Contact: [email protected]" 
"http://www.life.com" 
// etc... 

你不需要foreach循環。相反,訪問每一個元素的$follows->data直接:

// Remove the foreach loop 
$data = array(
    // Each property is directly accessible in $follows->data 
    'instagram_id' => $follows->data->id, 
    'followed_by_instgram_id' => $user_to_scrape['instagram_id'], 
    'user_name' => $follows->data->username, 
    'full_name' => iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($follows->data->full_name)), 
    'profile_picture' => $follows->data->profile_picture, 
    'followers' => $follows->data->counts->followed_by, 
    'follows' => $follows->data->counts->follows 
); 
printf("+ %s <%s>\n", $data['instagram_id'], $data['user_name']); 
    //skapa tabell med follows (instagram_id, 

if ($follows->data->counts->followed_by >= "30000") { 
    $app['db']->insert('follows', $data); 
} 

您可以創建引用data屬性,讓你用更少的代碼訪問這些內部性質的變量,但我不認爲這是必要的。

// Refer to data in $follow 
$follow = $follows->data; 
echo $follow->id; 
echo $follow->counts->followed_by; 
// etc...