You're overwriting the $keys
variable each time you go through your loop, so it's always only set to the last one.
$keys = array();
foreach ($user_rated_posts as $arrs) {
foreach($arrs as $key=> $value){
$keys[] = $key;
}
}
return $keys;
... that will return an actual array structure, if you actually want a comma separated list then return implode(', ', $keys);
instead.