Solved

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

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

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 look for a specific file type in a local or remote server directory using PHP.

627 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