[英]Insert with PHP associative array
I'm facing a little problem and I don't know is it's possible to achieve it or not.我面临一个小问题,我不知道是否有可能实现它。 Let me explain :让我解释 :
I have an associative array like this :我有一个像这样的关联数组:
Array
(
[res_id] => 104
[subject] => Test
[type_id] => 503
[format] => pdf
[typist] => fefe
[creation_date] => 2017-02-10 14:27:37.236711
[modification_date] => 2017-02-10 14:27:37.236711
[fulltext_result] => 1
[doc_date] => 2017-02-01 00:00:00
[docserver_id] => FASTHD_MAN
[path] => 2017#02#0001##
[filename] => 0008.pdf
[fingerprint] =>
[filesize] => 84979
[status] => VAL
[destination] => DSG
[priority] => 2
[is_multi_docservers] => N
[is_frozen] => N
[tablename] => res_letterbox
[initiator] => COU
[dest_user] => ddaull
[locker_user_id] => fefefef
[locker_time] => 2017-02-13 15:52:25.624521
[confidentiality] => N
[tnl_path] => 2017#02#0001##
[tnl_filename] => 0008.png
)
I want to know if I can use this associative array, in order to make an INSERT TO request ?我想知道我是否可以使用这个关联数组来发出 INSERT TO 请求? I want the first part of the array (like res_id, subject) goes to column part for the insertion.我希望数组的第一部分(如 res_id、subject)转到用于插入的列部分。 The second part of the array (like 104,Test) will go to the values数组的第二部分(如 104,Test)将转到值
Thanks in advance for your help, hope I am clear enough..在此先感谢您的帮助,希望我足够清楚..
@Nathan30 try this : @Nathan30 试试这个:
<?php
$arr = array(
"res_id" => 104,
"subject" => "Test",
"type_id" => 503,
"format" => "pdf",
"typist" => "fefe",
"creation_date" => "2017-02-10 14:27:37.236711",
"modification_date" => "2017-02-10 14:27:37.236711",
"fulltext_result" => 1,
"doc_date" => "2017-02-01 00:00:00",
"docserver_id" => "FASTHD_MAN",
"path" => "2017#02#0001##",
"filename" =>" 0008.pdf",
"fingerprint" => "",
"filesize" => 84979,
"status" => "VAL",
"destination" => "DSG",
"priority" => 2,
"is_multi_docservers" => "N",
"is_frozen" => "N",
"tablename" => "res_letterbox",
"initiator" => "COU",
"dest_user" => "ddaull",
"locker_user_id" => "fefefef",
"locker_time" => "2017-02-13 15:52:25.624521",
"confidentiality" => "N",
"tnl_path" => "2017#02#0001##",
"tnl_filename" => "0008.png",
);
echo "<pre>";
print_r($arr);
$column = array();
$values = array();
foreach($arr as $key => $value){
$column[] = $key;
$values[] = $value;
}
now query will be like:
"insert into table values(".implode(',', $column).") values (".implode(',', $values).")";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.