Php数组foreach

时间:2015-06-08 02:01:22

标签: php mysql arrays

我正在尝试从API获取一些数据并将其放入数组然后再放入MySQL。

我的代码:

$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: info@skogen.com"
    ["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"
  }
}

我收到的错误是:

  

注意:尝试在 40 /var/www/script.php 中获取非对象的属性

在第40行,我们有: 'instagram_id' => $follow->id, 我也在下面的数组上得到错误。

无法弄明白。

1 个答案:

答案 0 :(得分:3)

由于$follows->datastdClass对象,因此使用foreach进行迭代将分别遍历其每个属性,并返回每个属性的。这意味着尽管循环中存在id,但它只是循环的最后一个数据元素,其属性名称无法访问。

使用foreach$follow的迭代器值直接导致值而不是属性,如:

// Value of $follow on each loop iteration:
"Dimbos"
"•Have fun in life Contact: info@skogen.com"
"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...