Click here to Skip to main content
14,935,563 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to read multiple data and compare both data and also want to save it as new csv file
my code:
PHP
<?php

$handle = fopen("others.csv", "r");


// Now check file 2 for matches
$handle = fopen("new.csv", "r");

// Grab the header row from the file first
$header = fgetcsv($handle, 1000);

echo "<pre>Header data\n";
print_r($header);
header("content-type: text/plain");
$array = [];
$i = 0;
$csv1 = "new.csv";
$csv2 = "others.csv";

// Load file 1 into an array
// Skip row 1
if (($handle = fopen($csv1, "r")) !== FALSE){
    while (($data = fgetcsv($handle)) !== FALSE){
        if($i == 0){$i++; continue;}
        $array[] = $data;
        $i++;
    }
    fclose($handle);
}

$i = 0;
// Load file 2 into the array if the values don't exist
// Skip row 1


if (($handle = fopen($csv2, "r")) !== FALSE){
    while (($data = fgetcsv($handle)) !== FALSE){
        if($i == 0){$i++; continue;}
        $inarray = false;
        foreach($array as $itm){
            if(in_array($data[1], $itm)){
                $inarray = true;
                break;
            }
        }
        if(!$inarray){
            $array[] = $data;
        }
        $i++;
    }
    fclose($handle);

}


print_r($array);
?>
?>

new.csv:
ID product name Price offer price
1 Pampers Active Baby Small Size Diapers 153 99
2 Cosy Hooded Baby Towel 101 100
3 Baby Oil 102 101
4 sharp brush 103 102
5 Herbalife Afresh Energy Drink Mix - Lemon 104 103
6 Red Bull Energy Drink 542 104
7 San Marino White Grape Drink 106 105
8 Ankerite Glucose Energy Drink (Orange) 107 106
9 Paper Boat Kokum Drink Juice 108 107
10 Organa Fruit Drink Refreshing, Apple 109 108
11 Tropicana Pomegranate Delight Fruit Juice 250 222
12 Mantra Organic Mango Juice 251 223
13 Ceres Full Moon Harvest Juice 252 224
14 Lays Spanish Tomato Tango 253 225

others.csv:
ID product name brand Price offer price medium image status
1 Pampers Active Baby Small Size Diapers American Tourister 100 99 imagemedium.png Active
2 Cosy Hooded Baby Towel Cornitos 101 100 imagemedium.png Active
3 Baby Oil Falero 102 101 imagemedium.png Active
4 sharp brush Gala 103 0 imagemedium.png Active
5 Herbalife Afresh Energy Drink Mix - Lemon Gatorade 104 103 imagemedium.png Active
6 Red Bull Energy Drink Goodies 105 104 imagemedium.png Active
7 San Marino White Grape Drink Johnson& Johnson 106 105 imagemedium.png Active
8 Ankerite Glucose Energy Drink (Orange) Nestle 107 106 imagemedium.png Active
9 Paper Boat Kokum Drink Juice Palmer 108 107 imagemedium.png Active
10 Organa Fruit Drink Refreshing, Apple Red Bull 109 108 imagemedium.png Active
11 Tropicana Pomegranate Delight Fruit Juice Stute 250 222 imagemedium.png Active
12 Mantra Organic Mango Juice American Tourister 251 223 imagemedium.png Inactive
13 Ceres Full Moon Harvest Juice Cornitos 252 224 imagemedium.png Inactive
14 Lays Spanish Tomato Tango Falero 253 225 imagemedium.png Active
Posted
Comments
Mohibur Rashid 23-Sep-15 19:49pm
   
What third party tool are you using to read excel?

1 solution

I can tell the logic , never worked on PHP.
1-First go through all the files for the selected folder.
foreach (string f in Directory.GetFiles(txtInPutFolder))

2-Filer the excel file based on its extension.
if (f.ToLower().EndsWith(".xls") || f.ToLower().EndsWith(".xlsx"))

3-After getting the files you can perform any operation on it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900