dongzeao5047 2018-08-25 21:11
浏览 64

在PHP中保存CSV文件列

How to extract Column from CSV files in PHP.

#My data is like this, how to extract the country column and save it in array.

 a,b,c
 Andrew,US,23
 Bob,UK,21
 Stefani,UK,23
 Alen,UK,24

#CODE

$file = fopen("data.csv", "r");

while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
   print_r($row);   
 }

#Output:

  Array
  (
   [0] => a
   [1] => b
   [2] => c
  )
  Array
  (
   [0] => Andrew
   [1] => US
   [2] => 23
  )
  Array
  (
   [0] => Bob
   [1] => UK
   [2] => 21
  )
  Array 
  (
   [0] => Stefani
   [1] => UK
   [2] => 23
  )
  Array
  (
   [0] => Alen
   [1] => UK
   [2] => 24
  )

I have got the Data in array Format now how do i extract Country column.

  • 写回答

1条回答 默认 最新

  • douchen2595 2018-08-25 21:16
    关注

    Something like:

    $arr = [];
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
       array_push($arr,$row[1]);
     }
    
    $arr = array_unique($arr);
    

    If that's what you want to do. This should contain all countries (without duplicates).

    评论

报告相同问题?