Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-19
5
Medium Priority
?
359 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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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.
Suggested Courses

885 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