Automattic\WooCommerce\Blueprint

Util::array_to_insert_sqlpublic staticWC 1.0

Convert an array to an insert SQL query.

Метод класса: Util{}

Хуков нет.

Возвращает

false|Строку.

Использование

$result = Util::array_to_insert_sql( $row, $table, $type );
$row(массив) (обязательный)
Array row with key and value.
$table(строка) (обязательный)
Name of the table.
$type(строка)
One of insert, insert ignore, replace into.
По умолчанию: 'insert ignore'

Код Util::array_to_insert_sql() WC 9.9.5

public static function array_to_insert_sql( $row, $table, $type = 'insert ignore' ) {
	if ( empty( $row ) || ! is_array( $row ) ) {
		return false; // Return false if input data is empty or not an array.
	}

	$allowed_types = array( 'insert', 'insert ignore', 'replace into' );
	if ( ! in_array( $type, $allowed_types, true ) ) {
		return false; // Return false if input type is not valid.
	}

	// Get column names and values.
	$columns        = '`' . implode( '`, `', array_keys( $row ) ) . '`';
	$escaped_values = array_map( fn( $value ) => "'" . addslashes( $value ) . "'", $row );
	$values         = implode( ', ', $escaped_values );
	// Construct final SQL query.
	return "{$type} `$table` ($columns) VALUES ($values);";
}