3

I've got a csv file which contains product datas and prices from two distributors. There are 67 keys in this file. Now I want to search all EANs in this file which are twice available and then get the cheapest price. After that delete the other higher price product line. The CSV has a key for my merchant.

I made a test csv for easier view:

artno;name;ean;price;merchant
1;ipad;1654213154;499.00;merchant1
809;ipad;1654213154;439.00;merchant2
23;iphone;16777713154;899.00;merchant2
90;iphone;16777713154;799.00;merchant1

After the script runs through, the csv should look like (writing to new file):

artno;name;ean;price;merchant
809;ipad;1654213154;439.00;merchant2
90;iphone;16777713154;799.00;merchant1

I played around with fgetcsv, looping through the csv is not a problem, but how I can search for the ean in key 2?

$filename = './test.csv';
$file = fopen($filename, 'r');
$fileline = 1;

while (($data = fgetcsv($file, 0, ";")) !== FALSE) {
if($fileline == "1"){ $fileline++; continue; }


$search      = $data[2];
$lines       = file('./test.csv');
$line_number = false;

$count = 0;
while (list($key, $line) = each($lines) and !$line_number) {
   $line_number = (strpos($line, $search) !== FALSE) ? $key : $line_number;
   $count++;
}

if($count > 2){ 
    echo "<pre>",print_r(str_getcsv($lines[$line_number], ";")),"</pre>";
}

}
1
  • What have you tried so far? Where are you stuck? Commented Jan 9, 2020 at 10:37

1 Answer 1

3

I think this is what you are looking for:

<?php
$filename = './test.csv';
$file = fopen($filename, 'r');
$lines = file('./test.csv');
$headerArr = str_getcsv($lines[0], ";");

$finalrawData = [];
$cheapeastPriceByProduct = [];
$dataCounter = 0;

while (($data = fgetcsv($file, 0, ";")) !== FALSE) {
  if($dataCounter > 0) {
    $raw = str_getcsv($lines[$dataCounter], ";");
    $tempArr = [];
    foreach( $raw as $key => $val) {
      $tempArr[$headerArr[$key]] = $val;
    }
    $finalrawData[] = $tempArr;
  }
  $dataCounter++;
}

foreach($finalrawData as $idx => $dataRow ) {
  if(!isset($cheapeastPriceByProduct[$dataRow['name']])) {
    $cheapeastPriceByProduct[$dataRow['name']] = $dataRow;
  }
  else {
    if(((int)$dataRow['price'])< ((int)$cheapeastPriceByProduct[$dataRow['name']]['price'])) {
      $cheapeastPriceByProduct[$dataRow['name']] = $dataRow;
    }
  }
}

echo "<pre>";
print_r($finalrawData);
print_r($cheapeastPriceByProduct);

I just added $finalData data array to store the parsed data and associated all rows with their header key counterpart then you can compare and filter data based on your criteria.

Sign up to request clarification or add additional context in comments.

8 Comments

And how I get now the cheapest price?
I added in the last lines a loop where you can compare $dataRow['ean'] with a temp variable.
I added some code to compare EANs and get the cheapest price.
Perfect, but now I got only one result, there have to be two... These are two merchants and two products.
I updated the code, check it, the field check might be off but I will leave it for you to change what fields as you want, otherwise I will take your salary XD for this task.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.