First, try to use PDO instead. With prepared statements, your variables are sent separately from the query and you don't have to worry about manually escaping the parameters.
Second, if you REALLY need to do this, use mysqli_real_escape_string with array map. Something like this should work
$escapedArray = array_map(function($val) use ($mysqli) {
return is_string($val) ? $mysqli->real_escape_string($val) : $val;
}, $unescapedArray);
Or procedurally like this
$escapedArray = array_map(function($val) use ($mysqli) {
return is_string($val) ? mysqli_real_escape_string($mysqli, $val) : $val;
}, $unescapedArray);
The reason for this is simple. If any element of the array is not a string, escaping it will return null, so you return the element as is.
EDIT: For nested arrays, you will have to use array_walk_recursive, instead of array_map.
array_walk_recursive($varArray, function(&$val) use($mysqli) {
$val = !is_string($val) ?: mysqli_real_Escape_string($mysqli, $val);
});