【问题标题】:how to make an operation with php and two csv files using same ID如何使用相同的 ID 对 php 和两个 csv 文件进行操作
【发布时间】:2019-06-20 07:38:36
【问题描述】:

我是 php 新手,我希望用 php 处理两个不同的 csv 文件。

这是我的两个 csv 文件的屏幕截图: https://www.noelshack.com/2019-25-4-1561015547-csv1.png https://www.noelshack.com/2019-25-4-1561015547-csv2.png

在我的两个 csv 中,我有相似的 ID 和不同的价格(“Prix HT”列)。

我要做的是检查类似的ID,并用价格做一个操作。

例如,在 ID 96885 的 CSV1 中,我的价格为 40。 在相同 ID 的 CSV2 中,我有两个价格:45 和 48。

我在 php 中想要的是一种创建另一个列的方法,该列根据 csv1 文件计算 csv2 文件的价格差异。 在上面的示例中,代码的结果应该是 5 和 8。

我打算对大量 CSV 文件做这样的事情。

我只知道如何使用 fgetcsv 函数使 csv 文件出现在一个带有 php 的数组中,但我不知道下一步该怎么做。

这是我到目前为止的代码,不是那么多,因为我不知道下一步该怎么做:

<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>TITLE</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css">
    </head>

    <body>

    <?php

set_time_limit (0);
$testcsv = fopen('test.csv', 'a+');
$testcsv2 = fopen("test2.csv", "a+");
$fichierFinal = fopen('final.csv', 'w');

while($row=fgetcsv($testcsv, 99999,';')){
    $ID_product = $row[0];
    $price_product = $row[3];

    while($row1=fgetcsv($testcsv2, 99999,';')){
        $ID_combination = $row1[0];
        $price_combination = intval($row1[3]);



        if($ID_product == $ID_combination ){
            $fprice = $price_combination - $price_product;
            $row1[3]=$fprice;

        }
        fputcsv($fichierFinal, $row1, ";");
    };

}
?>

    </body>
</html>

【问题讨论】:

  • 你能把你目前拥有的代码包括进来,这样会更容易告诉你如何修改它来完成你想要的工作。
  • @NigelRen 我已经用我现在拥有的代码编辑了我的原始帖子
  • @NigelRen 我已经用我尝试过的东西编辑了我的代码,但它不起作用:/

标签: php csv fgetcsv


【解决方案1】:

我试过这个脚本。请注意,我使用了两个 CSV 文件,每个文件只有一列:

<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>TITLE</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css">
    </head>

    <body>

    <?php

set_time_limit (0);
$row1= array();
$row =array();
$rowFinal = array();
$testcsv = fopen('file1.csv', 'a+');
$fichierFinal = fopen('totale.csv', 'w');

while($row=fgetcsv($testcsv, 99999,';')){
    $ID_product = intval($row[0]);

    $testcsv2 = fopen('file2.csv', 'a+');
    while($row1=fgetcsv($testcsv2, 99999,';')){
        $ID_combination = intval($row1[0]);
        if($ID_product == $ID_combination ){


            $fprice = $ID_product + $ID_combination;
            //echo $fprice;
            $rowFinal[]= $fprice;
            echo $fprice . "  "; 
        }

    }
    fclose($testcsv2);

}

fputcsv($fichierFinal, $rowFinal, ";");

?>

    </body>
</html>

告诉我。

【讨论】:

  • 谢谢,但显然出了点问题,最终的 csv 文件是空的,当我 var_dump rowFinal var 时它的结果是:array(1) { [0]=> int(0) } array(1 ) { [0]=> int(0) } array(1) { [0]=> int(0) } array(1) { [0]=> int(0) } array(1) { [0] => int(0) } 数组(1) { [0]=> int(0) }
  • 我已使用经过测试的脚本更新了响应。告诉我
猜你喜欢
  • 2020-07-23
  • 1970-01-01
  • 2015-08-12
  • 2014-05-07
  • 2020-12-13
  • 1970-01-01
  • 2012-03-25
  • 2021-10-27
  • 1970-01-01
相关资源
最近更新 更多