while内部while循环仅在PHP首次运行时有效

时间:2019-05-09 15:55:16

标签: php arrays json foreach while-loop

我在里面做的时候,但是第二次只在第一次循环中工作

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');

$output_name = md5(uniqid()) . ".csv";
$output = ROOT . "/uploads/output/" . $output_name;
$fp = fopen($output, 'wb');
fputcsv($fp, array('campaign', 'spent', 'payout', 'profit', 'roi'), ',');
$found_campaigns = [];

fgetcsv($file1); // Skip first line
while (($data = fgetcsv($file1)) !== FALSE) {
    array_push($found_campaigns, $data[$campaign]);
    echo $data[$campaign]; //  Runned propertly
    echo "</br>";


    // Check if the second file have same campgain name with the first file campgain name if yes take the payout field and merge them with the spent field.
    $found_in_file_2 = false;
    $pout = '';
    while (($data2 = fgetcsv($file2)) !== FALSE) {
        echo "Runned <br>"; // RUNNED ONCE.
        if ($data2[$campaign2] == $data[$campaign]) {
            $found_in_file_2 = true;
            $pout = $data2[$payout];
        }
    }

    $pout = str_replace('$', '', $pout);

    $line = [
        $data[$campaign],
        $data[$spent],
        $pout,
        $pout - $data[$spent],
        ($pout - $data[$spent]) / $data[$spent]
    ];

    fputcsv($fp, $line, ',');
}

结果是:

ZroJumiaNGMob1
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
ZroJumiaNGMob2
ZroJumiaNGMob3
ZroJumiaNGMob4
ZroJumiaNGMob5
ZroJumiaNGMob6
ZroJumiaNGMob7
ZroJumiaNGMob7

但是结果必须是:

ZroJumiaNGMob1
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
ZroJumiaNGMob2
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned
ZroJumiaNGMob3
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned
ZroJumiaNGMob4
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned
ZroJumiaNGMob5
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned
ZroJumiaNGMob6
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned
ZroJumiaNGMob7
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned
ZroJumiaNGMob7
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned 
Runned

实际上,它需要在数据2中搜索数据1的活动名称(未在代码中显示),但是我确实找到了一种方法,可以在放入该行之前的一段时间内执行此操作 我不知道为什么它在一段时间内不在每个循环中运行 有人可以帮我吗

0 个答案:

没有答案