PHP Vlookup脚本

时间:2016-10-03 11:46:52

标签: php csv vlookup

我有下面的脚本,它像Excel中的vlookup()一样工作。

但是在这里我想获得与csv匹配的数据。

我创建了不同的脚本,但两者都不起作用。

脚本1

$output = fopen("/var/www/outputfile.csv", 'w');
fputcsv($output, array('Entity ID', 'Part Number', 'New Title', 'New Description','Model Info'));

function checkpartnumber($entity_id,$partno,$output) {
    $source  = fopen("/var/www/Encompass_new_data.csv", 'r');
    $counter = 0;
    while (!feof($source)) {
        $arr = fgetcsv($source);
        $psdata = str_replace("'","",$partno);
        $encompasspart = str_replace("'","",$arr[0]);
        if ($encompasspart == $psdata) {
            echo $psdata . "\n";
            fputcsv($output, array($entity_id,$psdata,$arr[2],$arr[3],$arr[4] ));
        }
    }
    fclose($source);
}

$destination  = fopen("/var/www/PS_Remote_Data.csv", 'r');
while($data = fgetcsv($destination)){
    foreach($data as $pspartnumber)
    {
            $entity_id = $pspartnumber[0];
            $partno = $pspartnumber[1];
            checkpartnumber($entity_id,$partno,$output);
    }
}

脚本2

$source  = fopen("/var/www/Tickets/14674/03 October/Encompass_Remote_Data.csv", 'r');
$destination = array_map('str_getcsv', file('/var/www/Tickets/14674/03 October/Remote_Data.csv'));

$output = fopen("/var/www/Tickets/14674/03 October/outputfile.csv", 'w');
fputcsv($output, array('Entity ID', 'Part Number', 'SKUCode', 'Title','URL','Vendor','New Title', 'New Description','Model Info'));
$counter = 0;
//if($counter > 0){
while ($data = fgetcsv($source)) 
{
        $partnumber= ltrim("'",$data[0]);
        $newtitle = trim($data[2]);
        $description = trim($data[3]);
        $modelinfo = trim($data[3]);
        foreach($destination as $value)
        {
                $value[1] = ltrim("'",$value[1]);
                if($value[1]==$partnumber) 
                {
                 echo $value[0] . "\n";
                 fputcsv($output, array($value[0],$partnumber,$value[2],$value[3],$value[4],$value[5],$newtitle,$description,$modelinfo));
                }
        }
}

我已在enter image description here

的帮助下创建了此脚本

如果两个零件文件的零件号匹配,我想将源文件零件号搜索到目标文件中,并将源和目标文件列复制到新的csv文件中。

源文件列

Entity_id   Part Number
13789832    70148018
13789901    01T25013Y08
13789907    AA59-10015R
13789960    01T45518Y01
13790021    PQ10779M-12
13790123    01T55446Y01

目标文件列

Part Number Title   Description Model Information
01E40601S01 Alpine Assembly Remocon/ex-10   Alpine Assembly Remocon/ex-10   <table width='100%'><tr><td>EX10</td></table>
01T25013Y08 Alpine Remote Control Dva5205   Alpine Remote Control Dva5205   <table width='100%'><tr><td>DVA5205</td><td>DHAS680</td></table>
01T25688Y02 Alpine Remote Control   Alpine Remote Control   <table width='100%'><tr><td>DVA5200</td></table>
01T35478Y05 Alpine Remote Cont Nven852a (Ru Alpine Remote Cont Nven852a (Ru <table width='100%'><tr><td>NVEN851A</td><td>NVEN852A</td><td>PKG700A</td></table>
01T45518Y01 Alpine Remote Control Rue4187.  Alpine Remote Control Rue4187.  <table width='100%'><tr><td>CDA7873</td><td>CDA7876</td><td>CDA7878</td><td>CDA7892</td></tr><tr><td>CVA1003</td></table>
01T55446Y01 Alpine Assembly Remocon Rue4190 Alpine Assembly Remocon Rue4190 <table width='100%'><tr><td>IVAD900</td><td>IVAD901</td></table>

我想将源文件部件号搜索到目标csv文件中(如果它与源文件中的 entity_id 匹配)Part Number Title     目标文件中的DescriptionModel Information应该复制到输出csv文件。

0 个答案:

没有答案