Php array foreach

I am trying to fetch some data from an API and put it into an array and then to MySQL.

My Code:

$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);
            } 
        }
    }

The Vardump returns this:

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"
  }
}

And the error I receive is this:

Notice: Trying to get property of non-object in /var/www/script.php on line 40

On line 40 we have this: 'instagram_id' => $follow->id, I also get error on the following set arrays.

Can't really figure it out.

Upvotes: 2

Views: 96

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270775

Because $follows->data is a stdClass object, iterating it with foreach will loop over each of its properties individually, returning the value of each property. This means that though id is present in the loop, it is merely the last data element of the loop, inaccessible by its property name.

Using the foreach, the iterator value of $follow results directly in the values rather than properties, as in:

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

You don't need the foreach loop. Instead, access each element of $follows->data directly:

// 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);
}

You could create a variable that references the data property, allowing you to access those inner properties with less code, but I don't see it as necessary.

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

Upvotes: 3

Related Questions