例如,我有7个频道和4个组。这应该按以下方式分发:
Channel 1 - Group 1
Channel 2 - Group 2
Channel 3 - Group 3
Channel 4 - Group 4
Channel 5 - Group 1
Channel 6 - Group 2
Channel 7 - Group 3
我如何以这种方式分发?
我使用了forloop如下
$groups = //array outputs the following
Array
(
[0] => Array
(
[id] => 10
[email] => susannec@methodfuel.com
)
[1] => Array
(
[id] => 17
[email] => air@tel.com
)
[2] => Array
(
[id] => 34
[email] => venu@methodfuel.com
)
[3] => Array
(
[id] => 62
[email] => varadaraj999@gmail.com
)
)
现在,
我正在查询
foreach ($query as $key => $value) {
$usrSql = "UPDATE issues SET user_id = ".$usersAssigned[$key]['id']." WHERE id =".$value['id'];
// DB updating process
}
对于4次迭代它工作正常,从第5次迭代我得到一个错误:
未定义的偏移量:4
如何解决这个问题?
四次迭代后循环应该从第一次开始。
答案 0 :(得分:0)
更新:您必须使用外部变量来跟踪正在访问的组元素的当前索引,并在它到达组末尾时将其重置为零。 / p>
这是你在找什么?
$channels = array("Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7");
$group_id = 0;
$groups = array( array("id"=>3, "email"=>"email_1"), array("id"=>10, "email"=>"email_2"), array("id"=>6, "email"=>"email_3"), array("id"=>71, "email"=>"email_4"));
foreach($channels as $channel)
{
echo $channel." - Group ".$groups[$group_id]["id"]."\n"; // print out the channel and the group id
$group_id ++;
if($group_id > 0 && $group_id % (count($groups)) == 0)
$group_id = 0;
}