StdClass object and array how to use php

I am trying to get the twelve identifiers this structure shows:

stdClass Object ( [checkins] => stdClass Object ( [count] => 12 [items] => Array ( [0] => stdClass Object ( [venue] => stdClass Object ( [id] => 4564654646456 . . 

I do:

 $checkins = $fsObjUnAuth->get("/users/self/checkins"); $count = $checkins ->response->checkins->count; // so I can get 12 for( $i = 0; $i < $count; $i ++) { $a1[] = $checkins['items'][$i]['venue']['id']; //two tries $a2[] = $checkins ->response->checkins->items->$i->venue->id; echo $i; echo ": "; echo $a1;echo"<br>"; echo $a2;echo"<br>" } 

But I understand: Fatal error: you cannot use an object of type stdClass as an array in a string.

Please someone show me how to do this?

thanks a lot

+4
source share
3 answers

You cannot access the members of an object using the array indexing operator [] .

You should use the operator -> :

 $x = new StdClass(); $x->member = 123; 

In your case, you have to use a mixture, since you have an object ( $checkins ) with a member ( $items ), which is an array that contains additional objects.

 $a1[] = $checkins->items[$i]->venue->id; 
+8
source

Here is a simple solution to convert stdClass Object to an array in php with get_object_vars function

Take a look: http://php.net/manual/fr/function.get-object-vars.php

Example:

 debug($array); $var = get_object_vars($array); debug($var); 

Or replace debug with print_r

I am using the CakePHP framework

Cdt

+2
source

change

 $a1[] = $checkins['items'][$i]['venue']['id']; 

changed to

 $a1[] = $checkins->items[$i]->venue->id; 
0
source

Source: https://habr.com/ru/post/1341839/


All Articles