That’s exactly what I meant, loop through the $_POST array, but you should add the $key as key:
$data = array();
foreach($_POST as $key => $value) {
$data[$key] = esc($value);
};
However, if $value can be an array as well, you would have to check if it is an array within the foreach loop and then use a nested foreach loop to add those values to the $data array. Also, you don’t want the submit key in it nor probably a token, if you are using one.