我有这个数组。
Array
(
[customer_id] => Array
(
[customer_id] => 132
)
[file_name] => Array
(
[0] => ht5appletv11.png
[1] => twitter-100px11.png
)
[file_path] => Array
(
[0] => C:/xampp/htdocs/maintenance_assurance_sys/uploads/ht5appletv11.png
[1] => C:/xampp/htdocs/myproject/uploads/twitter-100px11.png
)
)
我想在CI中逐行地将这些数据插入到数据库中。但是每行的customer_id总是相同的。 file_name和file_path将更改。 例如:
我该怎么做?
+---------------------------------------------------------------------+
| customer_id | file_name |file_path |
| 132 | ht5appletv11.png | C:/xampp/htodcs/..... |
| 132 | twitter-100px11.png | C:/xampp/htodcs/..... |
+---------------------------------------------------------------------+
这是我的代码......
function insertUploadedFiles($data)
{
foreach ($data as $fileData)
{
$datas[] = array(
'customer_id' => $fileData['customer_id'],
'file_name' => $fileData['file_name'],
'file_path' => $fileData['file_path']
);
}
$this->db->insert_batch('t_customer_file_ref', $datas);
}
但它只获得customer_id和file_name& file_path显示如下错误。
消息:未定义的索引:file_name
未定义的索引:file_path
答案 0 :(得分:3)
请尝试以下代码,
function insertUploadedFiles($data)
{
foreach ($data['file_name'] as $key => $fileData)
{
$datas[] = array(
'customer_id' => $data['customer_id']['customer_id'],
'file_name' => $fileData,
'file_path' => $data['file_path'][$key]
);
}
$this->db->insert_batch('t_customer_file_ref', $datas);
}
我假设file_name和file_path元素都有相同的索引,如0,1,2等。