如何更改数组头并将数组插入PHPExcel中的MySQL数据库?

如何更改数组头并将数组插入PHPExcel中的MySQL数据库?,php,arrays,codeigniter,phpexcel,sql-insert,Php,Arrays,Codeigniter,Phpexcel,Sql Insert,我有一段用于创建Excel导入(.xlsx)的代码。我有一个关于插入迭代数组和更改数组头的问题 控制器: $this->load->library('Excel'); $file = 'upload/keuangan.xlsx'; //read file from path $objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel->setReadDataOnly(true); $o

我有一段用于创建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);
}
这是keuangan.xlsx,它由一个字符串头组成。我从第二行选择。该列可以展开

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
的结果是:

Array
(
    [no_ijazah] => 1245/SA
    [jumlah] => 140000000
    [keluar_1] => 20000000
    [keluar_3] => 15000000
    [keluar_4] => 25000000
)
我正在尝试将这个
$ExcelData
放入我的数据库MySQL。我可以将
$ExcelData
插入我的数据库,但标题仍然是数字(
[0]
[1]
[2]
[3]
),因为
$row
$col
使用的
$getcellbycolumn和
都是数字

如何在头更改为后将这些数组插入数据库

$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);

希望这对您有所帮助:

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

见工作示例:

使用
array\u combine
添加键值对,如下所示:

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
        )
)
输出

$this->db->insert_batch('t_keuangan',$combined);
使用
insert\u batch
将数据插入表中

$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);
}

更多信息:

我看不到向数据库添加任何内容的代码。我添加了插入函数(已编辑)。如果我在这里出错,请原谅。我已经回答、评论并投票支持了答案。因为我的名声是负的,我不知道它是否被计算在内。如何使它们变成绿色?起初是keluar
1
2
3
,然后数字变了。。。它还显示
列可以展开。
-这需要扫描
$row=1
工作表中的新列名,然后检查数据库中是否已经存在这些列名,以及
ALTER TABLE
,以添加所需的新列,以防不存在。请帮我忙pradeep先生。但是在
set\u flashdata
save
功能中,它们应该在
$row
迭代中。而
$combined[]
应该是
$combined
,因为这个
保存
函数不能插入数组元素。这太好了,我非常感谢。我在这里使用的
insert\u批
insert
有什么区别吗?好的,马丁先生,我将尝试使用
ALTER TABLE
添加列如果您想插入单个记录,您应该在循环中使用
insert
,但是如果您想批量插入记录,您必须将
insert\u batch
放在循环之外
$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);
}