简体   繁体   中英

How to resolve Cannot use object of type stdClass as array error in php

Hello friends here is my code

foreach ($pageList as $page) {
    echo var_dump($page);
}

this is my output using var_dump

array(1) { [0]=> object(stdClass)#41 (2) { ["access_token"]=> string(182) "long string goes here" ["id"]=> string(15) "849929535061042" } } 

I want to get access token and id in the form of variable like $page['access_token']=$pageaccesstoken; . How can i do that from that output.

@CollinD was almost correct, you do need to use an "arrow operator", but you should do that on the 0th element of the array.

Let's look at your var_dump output:

array(1) { [0]=> object(stdClass)#41 (2) { ... } } 

What this means is that your $page is actually an array of size one, and the 0th element is the object of stdClass , and it is that class that has two fields.

The fields are named access_token and id . In order to access access_token you would write:

$page[0]->access_token;

You should use like that

                  `foreach ($pageList as $page) {
                       $arr = json_encode($page,true);
         $arr1 = json_decode($arr,true);
              //print_r($arr1);

              $pageaccesstoken=@$arr1[0]['access_token'];
  $id=@$arr1[0]['id'];`

to access page id and access token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM