stdClass Object In Array Iteration

admin

Administrator
Staff member
I've got an array of data containing stdClass Objects that looks like this if I do
Code:
print_r($results)
:

Code:
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
Code:
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:
Code:
$results[0]->ID
.

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:

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

    //$result->ID;
    $result[$i]['ID'];
    $i++

}

These return an error:

<blockquote>
Fatal error: Uncaught Error: Cannot use object of type stdClass as
array
</blockquote>

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="https://stackoverflow.com/questions...ect-of-type-stdclass-as-array-using-wordpress">&#39;Cannot use object of type stdClass as array&#39; using Wordpress</a>
- <a href="https://stackoverflow.com/questions/16287782/iterating-through-a-stdclass-object-in-php">iterating through a stdClass object in PHP</a>
- <a href="https://stackoverflow.com/questions/34175134/php-loop-stdclass-object">PHP Loop stdClass Object</a>

<strong><em>UPDATE</em></strong>
I'm getting this data as follows:

Code:
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 );

Thanks