The second loop should read;
PHP Code:
if ($r->sobjects) { //loop through related object and return fields, ie Account Name
$c = 0; //moved this variable
foreach($r->sobjects as $s) {
foreach($r->sobjects[$c]->fields as $key => $value) {
//watch out for overwriting fields of same name/key, ie account.name and contact.name
//append the sobject type to the key name
$field_ary[strtolower($r->sobjects[$c]->type.'.'.$key)]=$value.$field_id;
}
$c += 1;
}
}