当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Key-Value Array from csv with just two columns in spreadsheet

问题描述:

I have a csv, generated with Open Office Spreadsheet, separator is comma, text wrapper is ".

I want just to columns in my spreadsheet, as it is some key-value list,

like:

a -> b

aa -> bb

aaa -> bbb

so my spreadsheet has just two columns, each row containing a key-value pair, each key in a cell, each value in a cell. But when I use

if ($_FILES ['userfile'] ['error'] == UPLOAD_ERR_OK &&

is_uploaded_file ( $_FILES ['userfile'] ['tmp_name'] )) {

$csvfile = file_get_contents ( $_FILES ['userfile'] ['tmp_name'] );

}

$dict = str_getcsv($csvfile);

And var_dump $dict afterwards, i get

array(4) { [0]=> string(1) "a" [1]=> string(6) "b "aa"" [2]=>

string(8) "bb "aaa"" [3]=> string(3) "bbb" }

See index 1, where the first value "b" and the seconde key "aa" are stored in one index.

OpenOffice places no comma behind the values, aka before a new row.

How do I avoid that two strings end up in one index?

Can I somehow tell str_getcsv that when two " follow each other, it's also a separator? Or can I add a third column always containing # and say "if it contains #, its a line break? (I tried, and couldn't manage..)

Or is there even a function, that expects key, value, linebreak in csv?

Whats the correct way to do this? I'm sure people done this so often, but I don't know how to google it.

Thanks!

网友答案:

How about using fgetcsv() ? http://www.php.net/manual/en/function.fgetcsv.php

It reads line by line, there's an example in the link.

分享给朋友:
您可能感兴趣的文章:
随机阅读: