Solved

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

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