Solved

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

Posted on 2006-11-19
5
354 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 143

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 143

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 …

821 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