【问题标题】:How to change array header and insert the array into MySQL database in PHPExcel?如何更改数组标题并将数组插入 PHPExcel 中的 MySQL 数据库?
【发布时间】:2026-02-02 19:40:01
【问题描述】:

我有一段用于制作 Excel 导入 (.xlsx) 的代码。我有一个关于插入迭代数组和更改数组标题的问题。

控制器:

$this->load->library('Excel');
$file = 'upload/keuangan.xlsx';

//read file from path
$objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel->setReadDataOnly(true);

$objPHPExcel = $objPHPExcel->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();

$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); //dari 0

$data['nomor'] = $this->participant_model->get_dummy(false)->result_array();
$num = $this->participant_model->get_dummy(false)->num_rows();

for($row=2; $row <= $highestRow; ++$row){
    for($col=0; $col < $highestColumnIndex; ++$col){
        $ExcelData[$col] = $objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
    }
    echo "<pre>";print_r($ExcelData);echo "</pre>";
    $this->session->set_flashdata('message_alert','<div class="alert alert-success">Data berhasil dimasukkan</div>');
    $this->participant_model->save($ExcelData);
}

型号:

public function save($data_participant){
    $this->db->insert('t_keuangan',$data_participant);
}

这是 keuangan.xlsx,它包含一个字符串头。我从第二行中选择。该列可以展开。

No Ijazah   Jumlah      Keluar 1    Keluar 2    Keluar 3
1234/SH     100000000   21000000    19000000    18000000
2345/SK     120000000   16000000    19000000    13000000
1245/SA     140000000   20000000    15000000    25000000

$ExcelData 的结果是:

Array
(
    [0] => 1234/SH
    [1] => 100000000
    [2] => 21000000
    [3] => 19000000
    [4] => 18000000
)
Array
(
    [0] => 2345/SK
    [1] => 120000000
    [2] => 16000000
    [3] => 19000000
    [4] => 13000000
)
Array
(
    [0] => 1245/SA
    [1] => 140000000
    [2] => 20000000
    [3] => 15000000
    [4] => 25000000
)

我正在尝试将此$ExcelData 放入我的数据库 MySQL 中。我可以在我的数据库中插入$ExcelData,但标题仍然是数字([0][1][2][3]),因为$row$col 使用的$getCellByColumnAndRow 是数字.

表头改成后如何将这些数组插入数据库

Array
(
    [no_ijazah] => 1245/SA
    [jumlah] => 140000000
    [keluar_1] => 20000000
    [keluar_3] => 15000000
    [keluar_4] => 25000000
)

?

【问题讨论】:

  • 我没有看到向数据库添加任何内容的代码
  • 我添加了插入功能(已编辑)
  • 对不起,如果我在这里错了。我已经回答,评论并投票了答案。由于我的声誉是负数,我不知道它是否被计算在内。以及如何使它们变绿?

标签: php arrays codeigniter phpexcel sql-insert


【解决方案1】:

希望对您有所帮助:

您可以像这样更改您的$ExcelData 数据以插入数据库;

查看工作示例:https://eval.in/1026123

使用array_combine 像这样添加键值对:

$value1 = Array
(
    '0' => '1234/SH',
    '1' => 100000000,
    '2' => 21000000,
    '3' => 19000000,
    '4' => 18000000
);
$value2 = Array
(
    '0' => '2345/SK',
    '1' => 120000000,
    '2' => 16000000,
    '3' => 19000000,
    '4' => 13000000
);



$key = array('no_ijazah','jumlah','keluar_1','keluar_3','keluar_4');

$combined[] = array_combine($key, $value1);
$combined[] = array_combine($key, $value2);.

print_r($combined);
$this->participant_model->save($combined);

输出

Array
(
    [0] => Array
        (
            [no_ijazah] => 1234/SH
            [jumlah] => 100000000
            [keluar_1] => 21000000
            [keluar_3] => 19000000
            [keluar_4] => 18000000
        )

    [1] => Array
        (
            [no_ijazah] => 2345/SK
            [jumlah] => 120000000
            [keluar_1] => 16000000
            [keluar_3] => 19000000
            [keluar_4] => 13000000
        )
)

使用insert_batch将数据插入到表中

$this->db->insert_batch('t_keuangan',$combined);

整个代码应该是这样的:

$key = array('no_ijazah','jumlah','keluar_1','keluar_3','keluar_4');
for($row=2; $row <= $highestRow; ++$row)
{
    for($col=0; $col < $highestColumnIndex; ++$col)
    {
        $ExcelData[$col] = $objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
    }

    $combined[] = array_combine($key, $ExcelData);
}
$this->session->set_flashdata('message_alert','<div class="alert alert-success">Data berhasil dimasukkan</div>');
$this->participant_model->save($combined);

模型函数应该是这样的:

public function save($data_participant)
{
    $this->db->insert_batch('t_keuangan',$data_participant);
}

更多:http://php.net/manual/en/function.array-combine.php

【讨论】:

  • 起初它是 keluar 1,2,3,然后数字改变了......它也显示为 The column(s) may be expanded. - 这需要扫描 $row = 1 以获取新列 -表中的名称,然后检查这些列名是否已存在于数据库中,并检查ALTER TABLE,以添加所需的新列,以防万一。
  • 你帮助我的一天,先生。普拉迪普但在set_flashdatasave 函数中,它们应该在$row 迭代中。而$combined[] 应该是$combined,因为这个save 函数不能插入数组元素。这太好了,我很感激你。
  • 我这里用的insert_batchinsert有什么区别?
  • 好的先生。马丁,我将尝试使用 ALTER TABLE 添加列
  • 如果你想插入单个记录,你应该在循环中使用insert,但是如果你想批量插入记录,你必须把insert_batch放在循环之外