Going by the description for the $format
argument
An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. A format is one of ‘%d’, ‘%f’, ‘%s’ (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
So unless the type is important for your column structure you can leave it as string.
You can however create a dynamic format array if you want to, something like this.
$data = [
'This is a string', // string
55, // integer
3.7, // float (double)
true, // boolean
];
$format = array_map(function ($value) {
switch (gettype($value)) {
case 'string':
return '%s';
case 'integer':
return '%d';
case 'double':
return '%f';
default:
return '%s';
}
}, $data);
print_r($format);
You can test this on https://3v4l.org/
What we have here is a basic format builder based on the type of each array element, we always default to %s
, also there is no need for a break;
as we always return
.
You can create a function out of this if you see that you use this multiple times, that was you can keep your code DRY.