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.
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).