gpt4 book ai didi

PHP数组foreach

转载 作者:行者123 更新时间:2023-11-29 01:55:19 26 4
gpt4 key购买 nike

我正在尝试从 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"
}
}

我收到的错误是这样的:

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

在第 40 行我们有这个:'instagram_id' => $follow->id,我在以下集合数组上也遇到错误。

实在想不通。

最佳答案

因为 $follows->data 是一个 stdClass 对象,用 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...

关于PHP数组foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30700333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com