假设这是您想要处理事情的方式,并且您不想循环遍历数组或使用准备好的语句,我认为这应该可行。我重新格式化了您的代码,但整体逻辑仍然完好无损。我添加的逻辑基本上是创建两个单独的数组。一个用于数值。另一个用于文本值:
// Test data.
$orderID = 'testorderid';
$userID = 'testuserid';
$price = 7.99;
$quantity = 2;
// Create the text values array.
$insertArrText[] = array('order_id' => $orderID, 'user_id' => $userID);
// Create the numerical values array.
$insertArrNumerical[] = array('price' => $price, 'quantity' => $quantity);
// Get the array keys & merge them into one combined array.
$array_keys = array_merge(array_keys($insertArrText[0]), array_keys($insertArrNumerical[0]));
// Set the column names.
$colNames = "( " . implode(", ", $array_keys) . " )";
// Loop through the '$insertArrText'
foreach ($insertArrText as $key => $row ) {
// Set the numerical values.
$numerical_values = implode(", ", $insertArrNumerical[$key]);
// Set the text values.
$text_values = "'" . implode("', '", $row) . "'";
// Set the column values array.
$colValuesArr[] = "(" . $text_values . "," . $numerical_values . ")";
}
// Set the column values string.
$colValues = implode(", ", $colValuesArr);
// Create the MySQL query.
$strsql = "INSERT"
. " INTO tbl_orders " . $colNames
. " VALUES " . $colValues
;
// Echo the output for testing.
echo $strsql;
该脚本的输出是:
INSERT INTO tbl_orders ( order_id, user_id, price, quantity ) VALUES ('testorderid', 'testuserid',7.99, 2)
EDIT 这是使用gettype 检测字符串类型的重新设计。更加灵活和健壮。这当然是使用测试数据,但应该很容易适应现实世界的场景。
// Test data.
$orderID = 'testorderid';
$userID = 'testuserid';
$price = 7.99;
$quantity = 2;
// Create an array map based on strings & MySQL DB field values.
$array_map = array();
$array_map['orderID'] = 'order_id';
$array_map['userID'] = 'user_id';
$array_map['price'] = 'price';
$array_map['quantity'] = 'quantity';
// Create the text arrays.
$insertArrText = array();
$insertArrNumerical = array();
// Set arrays for text and numberical types.
$text_types = array('string');
$numerical_types = array('double','integer');
// Lopop through the array map & assign values based on type.
foreach ($array_map as $array_map_key => $array_map_value) {
if (in_array(gettype($$array_map_key), $text_types)) {
$insertArrText[0][$array_map_value] = $$array_map_key;
}
else if (in_array(gettype($$array_map_key), $numerical_types)) {
$insertArrNumerical[0][$array_map_value] = $$array_map_key;
}
}
// Get the array keys & merge them into one combined array.
$array_keys = array_merge(array_keys($insertArrText[0]), array_keys($insertArrNumerical[0]));
// Set the column names.
$colNames = "( " . implode(", ", $array_keys) . " )";
// Loop through the '$insertArrText'
foreach ($insertArrText as $key => $row ) {
// Set the numerical values.
$numerical_values = implode(", ", $insertArrNumerical[$key]);
// Set the text values.
$text_values = "'" . implode("', '", $row) . "'";
// Set the column values array.
$colValuesArr[] = "(" . $text_values . "," . $numerical_values . ")";
}
// Set the column values string.
$colValues = implode(", ", $colValuesArr);
// Create the MySQL query.
$strsql = "INSERT"
. " INTO tbl_orders " . $colNames
. " VALUES " . $colValues
;
// Echo the output for testing.
echo $strsql;
这段代码的输出是:
INSERT INTO tbl_orders ( order_id, user_id, price, quantity ) VALUES ('testorderid', 'testuserid',7.99, 2)