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

Posted

技术标签:

【中文标题】从 CSV 提供错误的数组关联以导入 mysql【英文标题】:Give wrong array association from CSV to import into mysql 【发布时间】:2016-08-06 11:34:02 【问题描述】:

我想将基于 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,所以我只是将这个库放入 application/libraries 中。

这是我的代码:

    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】:

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

不过,我会说,如果您只是想从 CSV 中获取一组值,则不需要引入依赖项,因为 php 内置了 CSV 解析引擎。这是您使用 fopen() 的示例和fgetcsv() 将输出一个数组,其中包含 JUST 值(即使用数字键):

/* USE DOWNLOADED LIBRARY */    

// $this->load->library('csvimport');
// $tryOne = $this->csvimport->get_array();


$csv = $upload_data['full_path'];
$tryOne = array();

if( file_exists( $csv ) )
    $file = fopen( $csv , 'r' ); // r flag is for readonly mode

    while( ( $line = fgetcsv( $file ) ) !== false )  // if line exists

        $tryOne[] = $line; // add to array

    

    fclose( $file );


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

【讨论】:

以上是关于从 CSV 提供错误的数组关联以导入 mysql的主要内容,如果未能解决你的问题,请参考以下文章

比较 php MySQL 查询结果数组以从其中一个数组中获取关联值

怎么把csv文件导入mysql

csv导入错误的Mysql排序规则/类型第1362行的CSV输入中的列计数无效

CSV 到 Mysql 导入脚本以匹配字段

将 CSV 导入 Mysql Python 3.x

Mysql从脚本导入