You get Object of class stdClass could not be converted to string after converting $ss to array because you have still an array of objects. I tried to solve your problem using recursive function. I have both stdClasses and values on different levels. Please, replace my definition of $ss with your query and try my code.
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$ss = new stdClass();
$ss->a = new stdClass();
$ss->a->a = new stdClass();
$ss->a->b = new stdClass();
$ss->b = new stdClass();
$ss->a->a->a = "!";
$ss->a->a->b = "T";
$ss->a->b->a = "E";
$ss->a->b->b = "X";
$ss->b->a = "T";
$ss->b->c = 1;
$string = recursive($ss, "");
fwrite($myfile, $string);
fclose($myfile);
function recursive($objects, $string) {
foreach($objects as $object) {
if ($object instanceof stdClass) {
$string = recursive((array) $object, $string);
} else {
$string .= implode(', ', (array) $object); //my example is to concatenate
}
}
return $string;
}
fwrite() expects parameter 2 to be string, array given