Я сделал, пока внутри, а второй, работая только в первом цикле
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
на самом деле нужно искать название кампании data1 (не показано в коде) в данных 2, но я не могу найти способ сделать это за некоторое время до того, как поставить строку, которую я не знаюпочему это не работает в каждом цикле в то время Может кто-нибудь помочь мне с этим