我有一个这种形式的数组
Array
(
[0] => Array
(
[type] => Textbox
[label] => Société
[name] => societe
[properties] => a:2:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}
[language] => FR
[weight] => 1
[nid] => 2
[options] => NULL
)
[1] => Array
(
[type] => Select
[label] =>
[name] => interesstedby
[properties] => a:3:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";s:5:"value";s:2:"-1";}
[options] => a:1:{s:7:"options";s:31:"-1|Je suis intéressé(e) par";}
[language] => FR
[weight] => 6
[nid] => 2
)
[2] => Array
(
[type] => Select
[label] =>
[name] => referrer
[properties] => a:3:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";s:5:"value";s:2:"-1";}
[options] => a:1:{s:7:"options";s:4:"Test";}
[language] => FR
[weight] => 7
[nid] => 2
[options] => NULL
)
[3] => Array
(
[type] => Textarea
[label] => Message
[name] => message
[properties] => a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}
[language] => FR
[weight] => 8
[nid] => 2
[options] => NULL
)
)
为了更改为INSERT INTO
种查询,我使用了这个函数:
function insert_contact($array){
$values = array();
foreach($array as $key => $value){
$columns = array_keys($value);
$values[] = "('" . implode("', '", array_values($value))."')";
}
$sql = "INSERT INTO `".'form' . "` (`".implode("`, `", $columns)."`)";
$sql .= " VALUES ".implode(',', $values) .";";
return $sql;
}
当你注意到我把key_values
放到表的列中。这里的问题是键的顺序,因为它生成了一个错误的sql文件(例如数组的第一个元素有[options]
在最后,其他人有相同的元素)我尝试ksort()
但仍然!
我的代码中的错误是什么?谢谢
当前的sql查询:
INSERT INTO `form` (`type`, `label`, `name`, `properties`, `language`, `weight`, `nid`,`options`) VALUES
('Textbox', 'Société', 'societe', 'a:2:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', 'FR', '1', '2',NULL),
('Select', '', 'interesstedby', 'a:3:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";s:5:"value";s:2:"-1";}', 'a:1:{s:7:"options";s:31:"-1|Je suis intéressé(e) par";}', 'FR', '2', '2',NULL),
('Select', '', 'referrer', 'a:3:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";s:5:"value";s:2:"-1";}', 'a:1:{s:7:"options";s:4:"Test";}', 'FR', '3', '2',NULL),
('Textarea', 'Message', 'message', 'a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', 'FR', '4', '2',NULL);
期待:
INSERT INTO `form` (`type`, `label`, `name`, `properties`, `language`, `weight`, `nid`,`options`) VALUES
('Textbox', 'Société', 'societe', 'a:2:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', 'FR', '1', '2',NULL),
('Select', '', 'interesstedby', 'a:3:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";s:5:"value";s:2:"-1";}', 'a:1:{s:7:"options";s:31:"-1|Je suis intéressé(e) par";}', 'FR', '2', '2'),
('Select', '', 'referrer', 'a:3:{s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";s:5:"value";s:2:"-1";}', 'a:1:{s:7:"options";s:4:"Test";}', 'FR', '3', '2'),
('Textarea', 'Message', 'message', 'a:3:{s:8:"required";b:1;s:11:"class_label";s:8:"col-md-4";s:5:"class";s:8:"col-md-8";}', 'FR', '4', '2',NULL),
答案 0 :(得分:1)
您需要以相同的方式对数组进行排序。你提到了ksort,所以我不知道为什么你没有设法解决它。所有数组都具有相同的列,即使它们是空的并且主数组使用隐含索引,因此您实际上只需要添加一行ksort($value);
。
function insert_contact($array){
$values = array();
foreach($array as $i => $row){
ksort($row); // <-------------------- here
if (!$i) $columns = array_keys($row); // only need to do this once
$values[] = "('" . implode("', '", $row)."')";
}
$sql = "INSERT INTO `".'form' . "` (`".implode("`, `", $columns)."`)";
$sql .= " VALUES ".implode(',', $values) .";";
return $sql;
}
$array = array(
array('description' => 'blah1', 'id' => 1,),
array('id' => 2, 'description' => 'blah2',),
array('description' => 'blah3','id' => 3, ),
array('id' => 4, 'description' => 'blah4',),
array('id' => 5, 'description' => 'blah5',),
);
echo insert_contact($array);
http://sandbox.onlinephpfunctions.com/code/d27eba052b7dc2a1d80d2200d18fd6b72cc2e3df
此外,没有必要使用array_values()
,你应该清理这些字段以防止sql注入,如果你还没有。
再说一遍,您通常会重复使用这样的函数,因此请创建function insert_bulk($table, $array)
。完整代码:http://sandbox.onlinephpfunctions.com/code/b329cd9ff2c4ee0946695a7dd15532c09e70f9f8
答案 1 :(得分:1)
这是一个处理并非所有行都存在所有键的解决方案。它首先通过并找到所有键。然后以相同的顺序生成值。如果行没有该键的值,则添加NULL。
$var = array(
array(
'type' => 'Type 1',
'options' => 'Options 1',
),
array(
'type' => 'Type 2',
'name' => 'Name 2',
),
);
$keys = array();
foreach ($var as $fields) {
$keys = array_merge($keys, $fields);
}
$keys = array_keys($keys);
$values = array();
foreach ($var as $fields) {
$field_values = array();
foreach ($keys as $key) {
$field_values[] = array_key_exists($key, $fields) ? '\''. $fields[$key] .'\'' : 'NULL';
}
$values[] = '('. implode(',', $field_values) .')';
}
$sql = 'INSERT INTO `table` (`'. implode('`,`', $keys) .'`) VALUES '. implode(',', $values);
var_dump($sql);
这里有一个演示:http://sandbox.onlinephpfunctions.com/code/fcd4e6af0891b30a181b22e80f9a8cb8e10551f0
答案 2 :(得分:-2)
这应该有效:
function insert_contact ($array) {
foreach ($array) {
$keys = array();
$values = array();
foreach($array as $key => $value) {
$keys[] = $key;
$values[] = $value;
}
$query = 'INSERT INTO `form`
(`'.implode('`, `', $keys).'`)
VALUES("'.implode('", "', $values).'")';
return $query;
}
}