Solved

Show only unique entries from column in CSV.  fgetscsv - Unique Array ?

Posted on 2006-11-19
5
349 Views
Last Modified: 2013-12-12
Hi there,

I've looked around on this topic and have tried 'unique_array' and a bunch of functions but each time I seem to get duplicates.

I have a file that looks like this

Cat1 | Cat2 | Cat3

and I read it with fgetscsv to create an array called $data


I can echo the first column in the CSV but if I want only unique entrys from $data[0] (i.e first column in csv) I cant do it.

<code>
$handle = fopen ("sections.csv","r");
      while ($data = fgetcsv ($handle, 5000, "|")) {

echo $data[0];
</code>
}

Help much appreciated.

-      Matt
0
Comment
Question by:ManicZoot
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17975236

$values = array();
$handle = fopen ("sections.csv","r");
while ($data = fgetcsv ($handle, 5000, "|")) {
  $values[$data[0]] = $data[0];
}

foreach ( $values as $value )
{
  echo $value;
}
0
 

Author Comment

by:ManicZoot
ID: 17975369
Thanks angel,

That worked but I cant apply it as I would like. You see I need to use the other $data[0] feilds. My attempt at coding this will probably give you an idea of what im trying to do.

Im trying to construct a CSV set up as

Base Section | Section |Section Level | Pagename

<code>

$section1 = array();
$section2 = array();
$section3 = array();
$handle = fopen ("sections.csv","r");
while ($data = fgetcsv ($handle, 5000, "|")) {
  $section1[$data[0]] = $data[0];
  $section2[$data[1]] = $data[1];
  $section3[$data[2]] = $data[2];
}
//Create first level sections
foreach ( $section1 as $section )
{
$startpagename = strtolower($section);
$trimpagename = trim($startpagename);
$killspaces = preg_replace("/\s+/", "_",$trimpagename);
$strippagename = preg_replace('/[^0-9a-z\-\_]/i','',$killspaces);
$pagename = preg_replace("/_+/", "_",$strippagename);

 
$data2 .= "\"$section |\",\"$section |\",\"1\",\"" . $pagename . ".htm\"</br>";
}
//Create second level sections
foreach ( $section2 as $section )
{
$startpagename = strtolower($section);
$trimpagename = trim($startpagename);
$killspaces = preg_replace("/\s+/", "_",$trimpagename);
$strippagename = preg_replace('/[^0-9a-z\-\_]/i','',$killspaces);
$pagename = preg_replace("/_+/", "_",$strippagename);

 
$data2 .= "\"$data[0] |\",\"$data[0] |$section |\",\"2\",\"" . $pagename . ".htm\"</br>";
}
//Create third level sections
foreach ( $section3 as $section )
{
$startpagename = strtolower($section);
$trimpagename = trim($startpagename);
$killspaces = preg_replace("/\s+/", "_",$trimpagename);
$strippagename = preg_replace('/[^0-9a-z\-\_]/i','',$killspaces);
$pagename = preg_replace("/_+/", "_",$strippagename);

 
$data2 .= "\"$data[0] |\",\"$data[0]|$data[1] |$section |\",\"3\",\"" . $pagename . ".htm\"</br>";
}
echo $data2;

</code>
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17975462
ok, let's change only a little bit my first suggestion:


$values = array();
$handle = fopen ("sections.csv","r");
while ($data = fgetcsv ($handle, 5000, "|")) {
  if (!isset($values[$data[0]])
  {
    $values[$data[0]] = $data;
  }
}

foreach ( $values as $value )
{ // here, $value contains the entire (first) row per $data[0]
  print_r($value);
}

0
 

Author Comment

by:ManicZoot
ID: 17975533
I tried this one but it it would not parse.. php error

unexpected '{'

It looks fine to me.

$values = array();
$handle = fopen ("sections.csv","r");
while ($data = fgetcsv ($handle, 5000, "|"))
{
  if (!isset($values[$data[0]]) { $values[$data[0]] = $data; }
}

foreach ( $values as $value )
{
print_r($value);
}

So does this script allow me to return... if say i I set  ->  $values[$data[1]] = $data;

I would then get all unique entries from field data[1] and could then create a line which includes data[0] for those unique rows

i.e

$data2 .= " this $value[1] is unique and shares a row with $value[0]";

M.
0
 

Author Comment

by:ManicZoot
ID: 17976136
Thanks Angel.. I got it

// Create Section Level 3
$values = array();
$handle = fopen ("sections.csv","r");
while ($datar = fgetcsv ($handle, 5000, "|")) {

    $values[$datar[2]] = $datar;
}

foreach ( $values as $value )
{
if (isset($value[3])) {
$value[3] = trim($value[3]);
$value[2] = trim($value[2]);
$value[1] = trim($value[1]);
$value[0] = trim($value[0]);
$startpagename = strtolower($value[2]);
$trimpagename = trim($startpagename);
$killspaces = preg_replace("/\s+/", "_",$trimpagename);
$strippagename = preg_replace('/[^0-9a-z\-\_]/i','',$killspaces);
$pagename = preg_replace("/_+/", "_",$strippagename);

 
$data2 .= "\"$value[0]|$value[1]|$value[2]|\",\"$value[2]\",\"3\",\"" . $pagename . ".htm\"</br>";
}
}
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now