![](/img/trans.png)
[英]ezSQL Object(stdClass) Cannot use object of type stdClass as array
[英]How access to Nested Arrays: Cannot use object of type stdClass as array
我目前正在從事一個項目,在該項目中,用戶的關注者以類似於下面的數組的形式分配給我: http : //pastebin.com/304iPm4L
數組:
object(stdClass)#2 (3) {
["pagination"]=> object(stdClass)#3 (2)
{
["next_url"]=> string(146) "DATA"
["next_cursor"]=> string(13) "DATA"
}
["meta"]=> object(stdClass)#4 (1)
{
["code"]=> int(200)
}
["data"]=> array(9)
{
[0]=> object(stdClass)#5 (4)
{
["username"]=> string(7) "twurked"
["profile_picture"]=> string(106) "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11055444_697242820403478_108851570_a.jpg"
["id"]=> string(9) "307611076"
["full_name"]=> string(20) "#twurked For Reposts"
}
[1]=> object(stdClass)#6 (4)
{
["username"]=> string(12) "itsmarziapie"
["profile_picture"]=> string(107) "https://igcdn-photos-b-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/10735354_634448103338889_1219780551_a.jpg"
["id"]=> string(9) "415505158"
["full_name"]=> string(15) "Marzia Bisognin"
}
[2]=> object(stdClass)#7 (4)
{
["username"]=> string(11) "briankgrubb"
["profile_picture"]=> string(107) "https://igcdn-photos-d-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-19/10518270_1508565329429347_713909002_a.jpg"
["id"]=> string(8) "28307611"
["full_name"]=> string(11) "Brian Grubb"
}
[3]=> object(stdClass)#8 (4)
{
["username"]=> string(16) "redbulladventure"
["profile_picture"]=> string(106) "https://igcdn-photos-d-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11007825_728249480627075_348216069_a.jpg"
["id"]=> string(10) "1721797894"
["full_name"]=> string(18) "Red Bull Adventure"
}
}
}
我想做的是獲取每個用戶的username
值並將其輸出到屏幕。
因此,我想到的是,我可以輕松地(或我認為...)遍歷結果並僅在每個嵌套數組中輸出username
值。
我使用以下代碼:
$i = 1;
while ($i <= 10) {
$instagram->getUserFollows('self',10)->data[$i]["username"];
echo "<br/>";
$i++;
}
$instagram->getUserFollows('self',10)
數組是您在pastebin中看到的。 通過查看我的嵌套數組,似乎我需要一個數字,然后需要["username"]
才能訪問每個用戶名。
從理論上講, $instagram->getUserFollows('self',10)->data["1"]["username"];
應該輸出itsmarziapie
但是,當我運行上面的代碼時,我得到了:
Cannot use object of type stdClass as array
此錯誤是什么意思,為什么我不能將我的username
數據循環到屏幕上?
此時,我已經進行了大約2.5個小時的故障排除,現在正在尋求大家的指導。 我知道有很多關於同一錯誤的問題,但是沒有一個像這個一樣包含嵌套數組,所以我有點迷失了。
感謝您幫助我指出正確的方向:)
您可以使用此:
//You request 10 objects but you don't know if there are really 10
$info = $instagram->getUserFollows('self',10);
//How many objects?
$qty = count($info->data);
/* You can't assume that there are 10 objects (I refer to your code,
* you have 9 objects and you try to access to 10 objects)
*/
for($i = 0; $i < $qty ; $i++) {
echo $info->data[$i]->username . "<br>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.