Reputation: 4601
My var_dump
on the variable $results
shows something like this
array (size=11)
0 =>
object(stdClass)[69]
public 'Tables_in_database-name-here' => string 'wp_commentmeta' (length=14)
1 =>
object(stdClass)[68]
public 'Tables_in_database-name-here' => string 'wp_comments' (length=11)
2 =>
object(stdClass)[67]
public 'Tables_in_database-name-here' => string 'wp_links' (length=8)
...
10 =>
object(stdClass)[59]
public 'Tables_in_database-name-here' => string 'wp_users' (length=8)
I'd like to be able to extract the table names out of the above structure, and list them in a comma separated form like this;
`wp_commentmeta,wp_comments,wp_links....,wp_users`
I got a brain freeze moment as to how to extract this data out of that structure though.
I tried quite a few options as you can follow it thru the code below - each ended uo with an error!
foreach ($results as $current_item):
/*
* $current_item is something like this:
*
* object(stdClass)[69]
public 'Tables_in_database-name-here' => string 'wp_commentmeta' (length=14)
*/
# echo $current_item;
# fires an error as "Catchable fatal error: Object of class stdClass could not be converted to string"
# echo $current_item[1];
# fires an error as "Cannot use object of type stdClass as array in..."
# echo array_values($current_item);
# fires an error as "array_values() expects parameter 1 to be array, object given in .."
# echo $current_item['Tables_in_database-name-here'];
#fires an error as "Cannot use object of type stdClass as array in "
how do you get that darn thing? :)
endforeach;
Upvotes: 2
Views: 1290
Reputation: 164729
Your main problem is the property name Tables_in_database-name-here
which can't be represented via the usual $obj->propertyName
means. PHP lets you use a string property name via $obj->{'invalid-variable-name-but-ok-as-a-property'}
.
I'd simply construct the string via implode()
and array_map()
, eg
$string = implode(',', array_map(function($result) {
return $result->{'Tables_in_database-name-here'};
}, $results));
Demo ~ https://eval.in/172247
Regarding the property name, you can also cast stdclass
objects to an array. For example...
$associativeArray = (array) $result;
// array(1) { ["Tables_in_database-name-here"] => string(14) "wp_commentmeta" }
$indexedArray = array_values($associativeArray);
// array(1) { [0] => string(14) "wp_commentmeta" }
Upvotes: 2