php - How to assign 1 user to 1 order id?
Solution:
Using a nested loop as you do, this will for each order form the file, output each user.
What you can do instead is use 1 loop (reading the input file) and modulus (%
) of the line number ($i
) and output a user per order...
$userCount = count($users);
$i = 0;
while ($data = fgetcsv($handle)) {
echo $data[0] .":". $users[$i % $userCount] . PHP_EOL;
$i++;
}
Answer
Solution:
For each order ID (data), you iterate over all the users. Instead, you could use something like this:
<?php
$users = array('13','15','16','16','17','20');
$handle = fopen($_FILES['csvfile']['tmp_name'], 'r');
// Iterate over the users.
foreach ($users as $user) {
if (($data = fgetcsv($handle)) !== null) {
echo $data['OrderId'] . ':' . $user;
}
}
?>
Iterate over the users, for each user, if there is still csv data, consume that csv data and print order ID and user ID.
Source