movieprodw
asked on
PHP fgetcsv Distinct Column Values
Hello,
I have the code below and I am able to view my csv file.
I have the value '$dealer_identifier_column ' and what I need to do is to pull the distinct values of that column in the csv file and echo them.
The csv files can have 5 different dealers on them so I really need to seporate it and only use the one that I need for the task.
I have the code below and I am able to view my csv file.
I have the value '$dealer_identifier_column
The csv files can have 5 different dealers on them so I really need to seporate it and only use the one that I need for the task.
$row = 1;
if (($handle = fopen($full_file_path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
"distinct values" is usually a server function like in MySQL. A CSV file is just that, a file, with no coding attached to it. If the values you are looking for are scattered throughout the file, you may have to read the whole file in, sort it by the field you want, then scan it to find out where it changes to get the 'distinct' values.
Please show us the test data set (your CSV file). Once we can see that, we can give you a tested and working code example. You don't have to post confidential data or a lot of data, just the minimum SSCCE needed for us to give you a demonstration.
ASKER
Hello,
I will let you know what I want to do so you can get a glimpse of my issue.
- I have about 40 clients that ftp me data and there are about 10 formats that they are sending, I have tried for years to get them to send me the same formats but they never do.
- I wanted to create a tool that I can go in and setup all of their accounts with their data info so I can import them. I currently have to create a special php file that handles each one, it is getting ridiculous and I don't want to do that anymore.
Attached is the csv example and #DealerId is the column I want to find the info off. I was thinking that if I could get that column in an array then I could sort it then find the distinct/unique ones
This is the code I am using to echo the entire csv file, hope this helps.
I will let you know what I want to do so you can get a glimpse of my issue.
- I have about 40 clients that ftp me data and there are about 10 formats that they are sending, I have tried for years to get them to send me the same formats but they never do.
- I wanted to create a tool that I can go in and setup all of their accounts with their data info so I can import them. I currently have to create a special php file that handles each one, it is getting ridiculous and I don't want to do that anymore.
Attached is the csv example and #DealerId is the column I want to find the info off. I was thinking that if I could get that column in an array then I could sort it then find the distinct/unique ones
This is the code I am using to echo the entire csv file, hope this helps.
$row = 1;
if (($handle = fopen($full_file_path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, $delimiter, '"')) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
dcmotors-s.csv
ASKER
I used this and it works.
Do you see anything wrong with this?
Do you see anything wrong with this?
$handle = fopen($full_file_path, 'r');
$headers = fgetcsv($handle, 0, $delimiter, '"');
while ($line = fgetcsv($handle, 0, $delimiter, '"')) {
$line = array_combine($headers, $line);
$did_array[] = $line[$dealer_identifier_column];
}
$did_unique = array_unique($did_array);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Ray,
There are several files so that one worked for the one I was using.
Matt
There are several files so that one worked for the one I was using.
Matt