1

I am trying to compare csv data against my array, so the csv ocuk has about 7000 lines and i want to only display data from the csv that matches the $sku array, the array will eventually have about 500 items and will go to database array later but for now i just want it to function correctly

<?php

$sku = array("WC-564-EK","WC-562-EK");
echo '<table>'; //start table
$ocuk = fopen("ocuk.csv", "r");
while (($csv = fgetcsv($ocuk, 10000, ',')) !== FALSE)
{
   foreach ($sku as $var)
   {
      for ($i=; $i<=10000; $ii)
      {
         if (strtolower ($var)==strtolower($csv[$i]))
         {
            echo '<tr><td>',$csv[0], //brand
            '</td><td>',$csv[1], //stockcode
            '</td><td>',$csv[2], //desc
            '</td><td>',$csv[3], //quantiy
            '</td><td>',$csv[4], //resellerprice
            '</td><td>',$csv[5], //ean
            '</td><td>',$csv[6], //mpn
            '</td></tr>';
          }
      }
  }
}
fclose($handle);
echo '</table>'; //end table

?>

All i get is blank output

Vikas Arora
  • 1,666
  • 2
  • 17
  • 38
  • Have you tried var_dump to make sure csv is converted to array properly? I'd include trim(strtolower($csv[$i])) if I were you. – J A Jul 19 '14 at 11:34
  • 3
    what is this supposed to do: `for ($i=; $i<=10000; $ii)`? And wouldnt it be easier to just use `in_array` or `array_intersect`? – Gordon Jul 19 '14 at 11:34
  • for ($i=; $i<=10000; $ii) i just realised a mistake in this bit of the code it should be $i++ not $ii but i am using it to increase the checking against the array as with one test in the array it works but not when i have 2 plus – Andrew Crawford Jul 19 '14 at 11:39
  • well fixing the $i++ has not fixed the output – Andrew Crawford Jul 19 '14 at 11:40

1 Answers1

0

Instead of that while/foreach/for/if check, try with

while (($csv = fgetcsv($ocuk)) !== FALSE)
{
    if (false === empty(array_intersect($sku, array_map('trim', $csv)))) {
        echo …
    }
}

As an alternative, consider

class RowContainsValuesFilter extends FilterIterator
{
    private $valuesToSearchFor;

    public function __construct(Iterator $iterator, array $valuesToSearchFor)
    {
        $this->valuesToSearchFor = $valuesToSearchFor;
        parent::__construct($iterator);
    }

    public function accept()
    {
        return !empty(
            array_intersect(
                $this->valuesToSearchFor,
                array_map('trim', $this->current())
            )
        );
    }
}

Encapsulating the filter logic this way, makes it easy to reuse the logic wherever you need it without having to put the comparison at the place you use it. This makes changing the logic at a later stage much easier. All you'd do with the above is this:

$sku = array("WC-564-EK","WC-562-EK");
$file = new SplFileObject("ocuk.csv");
$file->setFlags(SplFileObject::READ_CSV);
$filteredFile = new RowContainsValuesFilter($file, $sku);

foreach ($filteredFile as $row) {
    // this will only give rows for which the filter criteria apply
}

Reference:

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559