stdClass Object In Array Iteration


Staff member
I've got an array of data containing stdClass Objects that looks like this if I do

Array ( 
    [0] => stdClass Object ( 
        [ID] => 1 
    [1] => stdClass Object ( 
        [ID] => 2 
    [2] => stdClass Object ( 
        [ID] => 3 

I need to get the values of ID as a comma seperated string. To do this, I intially tried to do
implode(",", $results)
but this gave errors due to the stdClass Objects. So after a reasonable amount of reading and checking SO etc, I've got to a point where I can access the value of ID on a given record:

However, I don't know how many rows there will be due to this data coming from a DB query. So, I need to iterate through each row and add this to a string.

I -amongst other things- tried this:

$i = 0;
foreach ($results as $result){



These return an error:

Fatal error: Uncaught Error: Cannot use object of type stdClass as

I've literally no idea at this point how to get all the ID values as a comma seperated string.

I've checked out numerous SO posts include the following:
- <a href="">&#39;Cannot use object of type stdClass as array&#39; using Wordpress</a>
- <a href="">iterating through a stdClass object in PHP</a>
- <a href="">PHP Loop stdClass Object</a>

I'm getting this data as follows:

global $wpdb;    
$query = "Select wp_users.ID from wp_users where wp_users.ID not in ( select wp_usermeta.user_id from wp_usermeta where wp_usermeta.meta_key = 'grp2_profile_visiblity' and wp_usermeta.meta_value = 1 order by wp_usermeta.user_id ) order by wp_users.ID"; 
$results = $wpdb-&gt;get_results( $query, OBJECT );