I've got an array of data containing stdClass Objects that looks like this if I do print_r($results):
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: $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:
$i = 0;
foreach ($results as $result){
//$result->ID;
$result[$i]['ID'];
$i++
}
These return an error:
Fatal error: Uncaught Error: Cannot use object of type stdClass as array
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: - 'Cannot use object of type stdClass as array' using Wordpress - iterating through a stdClass object in PHP - PHP Loop stdClass Object
UPDATE 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->get_results( $query, OBJECT );
Thanks
ibeing used which I'm guessing you meant to be$iwhich doesn't make sense given the data you've shown. You may want to read up on howforeachworks, the idea is to get rid of the iteration counter used in old fashionedforloops.