wpdb::process_field_formats()protectedWP 4.2.0

Prepares arrays of value/format pairs as passed to wpdb CRUD methods.

Method of the class: wpdb{}

No Hooks.

Return

Array. Array of values and formats keyed by their field names.

Usage

// protected - for code of main (parent) or child class
$result = $this->process_field_formats( $data, $format );
$data(array) (required)
Array of values keyed by their field names.
$format(string[]|string) (required)
Formats or format to be mapped to the values in the data.

Changelog

Since 4.2.0 Introduced.

wpdb::process_field_formats() code WP 6.5.2

protected function process_field_formats( $data, $format ) {
	$formats          = (array) $format;
	$original_formats = $formats;

	foreach ( $data as $field => $value ) {
		$value = array(
			'value'  => $value,
			'format' => '%s',
		);

		if ( ! empty( $format ) ) {
			$value['format'] = array_shift( $formats );
			if ( ! $value['format'] ) {
				$value['format'] = reset( $original_formats );
			}
		} elseif ( isset( $this->field_types[ $field ] ) ) {
			$value['format'] = $this->field_types[ $field ];
		}

		$data[ $field ] = $value;
	}

	return $data;
}