?
Solved

PHP fgetcsv Distinct Column Values

Posted on 2015-01-23
6
Medium Priority
?
491 Views
Last Modified: 2015-01-23
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.

$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);
	}

Open in new window

0
Comment
Question by:movieprodw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40566030
"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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40566176
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.
0
 
LVL 1

Author Comment

by:movieprodw
ID: 40566678
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.

$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);
	}

Open in new window

dcmotors-s.csv
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:movieprodw
ID: 40566919
I used this and it works.

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

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40567533
No, I don't see anything wrong with it, and if it works, that's great.  However the code does not match the data set posted here, since the delimiter is not a double quote, but instead a semi-colon.  Here is a tested and working code sample using the test data set.
http://iconoun.com/demo/temp_movieprodw.php

<?php // demo/temp_movieprodw.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28602334.html
 */
error_reporting(E_ALL);

$url = 'http://filedb.experts-exchange.com/incoming/2015/01_w04/894064/dcmotors-s.csv';
$fpr = fopen($url, 'r');
if (!$fpr) trigger_error("FAILED TO OPEN $fpr", E_USER_ERROR);

// WHAT IS THE SIGNAL OF THE DESIRED COLUMN?
$sig = '#DealerId';

// GET THE TOP ROW
$top = fgetcsv($fpr, 0, ';');

// GET THE POINTER TO THE SIGNAL
$set = FALSE;
foreach ($top as $ptr => $str)
{
    if ($sig == $str)
    {
        $set = TRUE;
        break;
    }
}
if (!$set) trigger_error("SIGNAL STRING $sig NOT FOUND IN TOP ROW OF $fpr", E_USER_ERROR);

// NOW $ptr POINTS TO THE ARRAY POSITION WE WANT
while (!feof($fpr))
{
    $arr = fgetcsv($fpr, 0, ';');
    if (empty($arr[$ptr])) continue;
    $out[$arr[$ptr]] = $arr[$ptr];
}

// SHOW THE WORK PRODUCT
echo '<pre>';
echo PHP_EOL . "UNIQUE VALUES FOR $sig IN COLUMN $ptr OF $url";
echo PHP_EOL;
print_r(array_values($out));

Open in new window

0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40567808
Thanks Ray,

There are several files so that one worked for the one I was using.

Matt
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question