我有一個CSV文件看起來像這樣:轉換CSV到數組
Did,status
"123","Active"
"456","Not-Active"
"789","Active"
....and so on
我希望能夠將其轉換成一個數組,看起來像這樣:
$DidStatus = array("123"=>"Active", "456"=>"Not-Active", "789"=>"Active");
我試過,但它不是我要找:
$file = file_get_contents("test.csv");
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file));
print_r($data);
但輸出不TH Ë一個我在尋找:
Array
(
[0] => Array
(
[0] => Did
[1] => status
)
[1] => Array
(
[0] => 123
[1] => Active
)
[2] => Array
(
[0] => 456
[1] => Not-Active
)
[3] => Array
(
[0] => 789
[1] => Active
)
[4] => Array
(
[0] =>
)
)
你嘗試過這麼遠嗎?你卡在哪裏? –
[如何使用PHP和fgetcsv函數從CSV文件創建數組](https://stackoverflow.com/questions/1269562/how-to-create-an-array-from-a-csv- file-using-php-and-the-fgetcsv-function) –
你看過http://php.net/manual/en/function.fgetcsv.php – IncredibleHat