2013-05-17 55 views
3

我有字符串的CSV文件數據轉換到一個數組等,其是使用fputcsv使用PHP str_getcsv功能

Date,Name,Hours 2013-01-02,"Test User",7:59 2013-01-03,"Test User",7:53 2013-01-04,"Test User",8:12 2013-01-07,"Test User",7:56 2013-01-08,"Test User",8:25 2013-01-09,"Test User",7:56 2013-01-10,"Test User",8:10 2013-01-11,"Test User",7:53 2013-01-14,"Test User",7:54 2013-01-15,"Test User",0:34 2013-04-01,"Test User",5:51 2013-04-02,"Test User",8:50 2013-04-03,"Test User",7:25 2013-04-04,"Test User",8:3 2013-04-05,"Test User","10:42:52[Not punch out]" ,Total,103:1 

創建時適當的報頭(報頭(「內容類型:應用/ CSV」);頭部(」 Content-Disposition:attachment; filename =「'。$ filename。'」');)設置它會給出正確的csv文件。

Converting csv files data to an array using php str_getcsv

但我想這個字符串再轉換爲一個陣列格式,所以我想通過上面的字符串到str_getcsv但得到陣列這樣

Array 
(
    [0] => Date 
    [1] => Name 
    [2] => Hours 
2013-01-02 
    [3] => "Allen Herrera" 
    [4] => 7:59 
2013-01-03 
    [5] => "Allen Herrera" 
    [6] => 7:53 
2013-01-04 
    [7] => "Allen Herrera" 
    [8] => 8:12 
2013-01-07 
    [9] => "Allen Herrera" 
    [10] => 7:56 
2013-01-08 
    [11] => "Allen Herrera" 
    [12] => 8:25 
2013-01-09 
    [13] => "Allen Herrera" 
    [14] => 7:56 
2013-01-10 
    [15] => "Allen Herrera" 
    [16] => 8:10 
2013-01-11 
    [17] => "Allen Herrera" 
    [18] => 7:53 
2013-01-14...................... 

你能幫我得到正確格式化數組,以便我可以輕鬆地使用它在數組上創建表循環。

+0

我認爲你需要'爆炸()'它使用'PHP_EOL'作爲分隔符,然後在'str_getcsv使用的每個部分()' –

回答

3

後一些搜索了這個

$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $string_data)); 
print_r($data); 

解決方案,使得ME陣列這樣

Array 
(
    [0] => Array 
     (
      [0] => Date 
      [1] => Name 
      [2] => Hours 
     ) 

    [1] => Array 
     (
      [0] => 2013-01-02 
      [1] => Test User 
      [2] => 7:59 
     ) 

    [2] => Array 
     (
      [0] => 2013-01-03 
      [1] => Test User 
      [2] => 7:53 
     ) 

    [3] => Array 
     (
      [0] => 2013-01-04 
      [1] => Test User 
      [2] => 8:12 
     ) 

    [4] => Array 
     (
      [0] => 2013-01-07 
      [1] => Test User 
      [2] => 7:56 
     ) 

    [5] => Array 
     (
      [0] => 2013-01-08 
      [1] => Test User 
      [2] => 8:25 
     ) 

    [6] => Array 
     (
      [0] => 2013-01-09 
      [1] => Test User 
      [2] => 7:56 
     ) 

    [7] => Array 
     (
      [0] => 2013-01-10 
      [1] => Test User 
      [2] => 8:10 
     )... 
2

這將是這樣的:

$parts = explode(PHP_EOL, $string); 
$array = array(); 
foreach ($parts as $part) { 
    $array[] = str_getcsv($part); 
} 

print_r($array); 
+0

未經測試,但我認爲這將工作。 –

+0

如果'PHP_EOL'不起作用,請嘗試使用不同的分隔符('「\ n」','「\ r \ n」','「\ r」')。 –

5
$file = file_get_contents("test.csv"); 
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file)); 
print_r($data); 
+0

這不會給我陣列我完全想要的。 –

+1

http://stackoverflow.com/questions/7502370/converting-csv-to-array –

2

我用這樣的方式轉換csv文件數據t Ø數組: -

ini_set('auto_detect_line_endings', TRUE);/// (PHP's detection of line endings) write at the top. 


$csvrows = array_map('str_getcsv', file($filepath)); 
$csvheader = array_shift($csvrows); 
$csv = array(); 
foreach ($csvrows as $row) { 
    $csv[] = array_combine($csvheader, $row); 
} 
-1
$path = 'uploads/my_csvfile.csv'; 
$file_content = file_get_contents($path); 
$matches = array_map('str_getcsv', preg_split('/\r*\n+|\r+/',$file_content)); 
print_r($matches); 
+0

請試試這個 –