从CSV提供错误的数组关联以导入到mysql

时间:2016-04-14 18:27:40

标签: php mysql codeigniter csv

我想基于csv上传的文件插入到mysql中。所以,我需要在codeigniter上将它们转换为insert_batch格式数组,如下所示:

$data = array(
    array(
            'title' => 'My title',
            'name' => 'My Name',
            'date' => 'My date'
    ),
    array(
            'title' => 'Another title',
            'name' => 'Another Name',
            'date' => 'Another date'
    )
 );

$this->db->insert_batch('mytable', $data);

csv看起来像这样(带制表符分隔符),例如,只有两个记录

4   A   B   C   D   E   F`  10.00   20.12   100 G   H   I   98  99  989 989 s   d   0   0           0   0   0   0
6   A   B   C   D   E   G   10.00   20.12   100 G   H   I   98  99  989 989 s   d   0   0           0   0   0   0

我使用此库:Csvimport

我不熟悉使用sparks,所以我只把这个库放到应用程序/库中。

所以这是我的代码:

    if (!empty($_FILES)) {
        $config['upload_path'] = "./assets/uploads";
        $config['allowed_types'] = 'csv';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload("file")) {
            echo "File cannot be uploaded";
        } else {
            $upload_data = $this->upload->data();
            $csv = $upload_data['full_path'];

            /*USE DOWNLOADED LIBRARY*/    

            $this->load->library('csvimport');
            $tryOne = $this->csvimport->get_array($upload_data['full_path']);

            echo "<pre>";
            print_r($tryOne);

        }
    } elseif ($this->input->post('file_to_remove')) {
        $file_to_remove = $this->input->post('file_to_remove');
        unlink("./assets/uploads/" . $file_to_remove);
    } else {
        $this->listFiles();
    }

结果如下:

Array
(
[0] => Array
    (
        [4] => 6
        [A] => A
        [B] => B
        [C] => C
        [D] => D
        [E] => E
        [F`] => G
        [10.00] => 10.00
        [20.12] => 20.12
        [100] => 100
        [G] => G
        [H] => H
        [I] => I
        [98] => 98
        [99] => 99
        [989] => 989
        [s] => s
        [d] => d
        [0] => 0
        [] => 
    )

这是问题,任何解决方案.. 谢谢你的解决方案,

现在我得到了这样的数组:

Array
(
[0] => Array
    (
        [0] => 4
        [1] => A
        [2] => B
        [3] => C
        [4] => D
        [5] => E
        [6] => F`
        [7] => 10.00
        [8] => 20.12
        [9] => 100
        [10] => G
        [11] => H
        [12] => I
        [13] => 98
        [14] => 99
        [15] => 989
        [16] => 989
        [17] => s
        [18] => d
        [19] => 0
        [20] => 0
        [21] => 
        [22] => 
        [23] => 0
        [24] => 0
        [25] => 0
        [26] => 0
    )

[1] => Array
    (
        [0] => 6
        [1] => A
        [2] => B
        [3] => C
        [4] => D
        [5] => E
        [6] => G
        [7] => 10.00
        [8] => 20.12
        [9] => 100
        [10] => G
        [11] => H
        [12] => I
        [13] => 98
        [14] => 99
        [15] => 989
        [16] => 989
        [17] => s
        [18] => d
        [19] => 0
        [20] => 0
        [21] => 
        [22] => 
        [23] => 0
        [24] => 0
        [25] => 0
        [26] => 0
    )

 )

这是真正的数组但是如何更改索引:

Array
(
[0] => Array
    (
        [ID] => 4
        [PIBAJU] => A
        [JIKA] => B
        [SAJA] => C
        [BISA] => D
        [DATA] => E
        [LIMA] => F`
        [DELAPAN] => 10.00
        [SEMBILAN] => 20.12
        [SEPULU] => 100
        [SEBELAS] => G
        [DUA BELAS] => H
        [TIGABELAS] => I
        [DUNIA] => 98
        [KISAH] => 99
        [CINTA] => 989

    )

[1] => Array
    (
         so on, so on
    )

1 个答案:

答案 0 :(得分:2)

通常,使用CSV解析引擎时,第一组数据将保留为字段的表格标题。这意味着此处的第一行充当了为其分配值的关联数组中的混合键。我假设如果你在这里有第三行值,它将具有与数组中第一项相同的混合键。

我会说,但是,如果您只是想从CSV中获取一组值,则不需要引入依赖项,因为PHP内置了一个CSV解析引擎。以下是使用{{1和fopen()将吐出一个JUST数组的数组(即使用数字键):

fgetcsv()