简体   繁体   English

在PHP中循环或循环分配

[英]looping or round robin assignment in php

I have a Customers and Employees table: 我有一个客户和雇员表:

Customers
    .account_no
    .zipcode
-----------------------
account_no  |  zipcode
-----------------------
1111000100  |  12345
1222000100  |  25678
1333000100  |  47890
1444000100  |  50000
1555000100  |  53456
1666000100  |  63987

Employees
    .id_e
    .zipcode_a
    .zipcode_b
    .card_id
---------------------------------------------
id_e  |  zipcode_a  |  zipcode_b  |  card_id
---------------------------------------------
1     |   10000     |   64000     |  1011
2     |   10000     |   54000     |  1002
3     |   50000     |   64000     |  1003

Let say I have 6 Customers, 3 Employees; 假设我有6位客户,3位员工; query result will be like this: 查询结果将是这样的:

---------------------------------
account_no  |  zipcode  |   id_e
---------------------------------
1111000100  |    12345  |     2
1111000100  |    12345  |     1
1222000100  |    25678  |     2
1222000100  |    25678  |     1
1333000100  |    47890  |     2
1333000100  |    47890  |     1
1444000100  |    50000  |     2
1444000100  |    50000  |     3
1444000100  |    50000  |     1
1555000100  |    53456  |     2
1555000100  |    53456  |     3
1666000100  |    63987  |     3
1666000100  |    63987  |     1

I would like to assign these customers account to each employees in looping or round robin to look something like this: 我想将这些客户帐户循环或循环分配给每个员工,如下所示:

---------------------------------
account_no  |  zipcode  |   id_e
---------------------------------
1111000100  |    12345  |     2
1222000100  |    25678  |     1
1333000100  |    47890  |     2
1444000100  |    50000  |     1
1555000100  |    53456  |     3
1666000100  |    63987  |     1

I have been trying using usual looping but the result is random and not desirable. 我一直在尝试使用常规循环,但是结果是随机的,是不可取的。

Any ideas, people? 有什么主意吗?

You can make an array and override the previous key to make it so every zip code just loads the last id_e to have it for example: 您可以创建一个数组并覆盖前一个键来创建它,因此每个邮政编码仅加载最后一个id_e即可,例如:

if you create an array with $myarray = array(); 如果使用$ myarray = array()创建一个数组; and in your query when you import the values from your database into your array... I would give you the code for that but I do not see how you are importing it; 在查询中,当您从数据库中将值导入到数组中时...我会给您提供代码,但是我看不到您如何导入它; if you show that I can help you properly format it for this case. 如果您证明我可以在这种情况下正确设置格式。

Then this is where you override duplicate cases of zipcodes and only take the last id_e with that same zipcode. 然后在这里您将覆盖邮递区号的重复情况,并且仅使用具有相同邮递区号的最后一个id_e。 foreach($myarray as $zipcode => $id_e){ $myarray[$zipcode] = $id_e; foreach($ myarray as $ zipcode => $ id_e){$ myarray [$ zipcode] = $ id_e; } }

Also I assume you currently are but make sure your mysql query includes " ORDER BY id_e" at the end of it. 我也假设您当前是,但是请确保您的mysql查询在其末尾包含“ ORDER BY id_e”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM