PHP - 如何使用两个数组的 foreach 循环来构建网格而不在行中重复值

发布于 2024-12-14 19:58:23 字数 1163 浏览 0 评论 0原文

我正在尝试建立一个办公室预约网格。 每行只应列出每个员工(用户)一次。 但每一列都应该代表一个约会。 用户的预约数量将不断变化。我的查询当然会为每个返回一行。我尝试的每一种方法最终都会为每个用户排成一行,每行只有一个约会。

我的数组

$users[] = array ('id' => $row ['id'], 'fname' => $row ['fname']);

输出:

Array ( 
[0] => Array ( [id] => 1 [fname] => Brent ) 
[1] => Array ( [id] => 2 [fname] => Macy )
      ) 

$appointments[] = array('id' => $row['id'], 'clientid' => $row['clientid'], 'userid' => $row['userid'], 'date' => $row['date']);

输出:

Array ( 
[0] => Array ( [id] => [clientfname] => [userid] => [date] =>) 
[1] => Array ( [id] =>10 [clientfname] =>Lisa [userid] =>1 [date] =>2011-11-10 1:00:00) 
[2] => Array ( [id] =>11 [clientfname] =>Amanda [userid] =>1 [date] =>2011-11-10 1:03:00) 
[3] => Array ( [id] =>12 [clientfname] =>Britany [userid] =>2 [date] =>2011-11-10 1:00:00) 
      )

(Employee Calendar)
<< Monday Oct 8 >>
<<  1:00pm  1:30pm  2:00pm  2:30pm  >>
Macy    Lisa    Amanda              
Vanessa     Britany

有什么想法吗???

I am trying to build an office appointment Grid.
Each row should only list each employee(user) ONCE.
But each column should represent an appointment.
Users will have an ever changing number of appointments. My query will of course return a row for each. Every way I try this ends up in a row for each user with only one appointment on each row.

My arrays

$users[] = array ('id' => $row ['id'], 'fname' => $row ['fname']);

output:

Array ( 
[0] => Array ( [id] => 1 [fname] => Brent ) 
[1] => Array ( [id] => 2 [fname] => Macy )
      ) 

$appointments[] = array('id' => $row['id'], 'clientid' => $row['clientid'], 'userid' => $row['userid'], 'date' => $row['date']);

output:

Array ( 
[0] => Array ( [id] => [clientfname] => [userid] => [date] =>) 
[1] => Array ( [id] =>10 [clientfname] =>Lisa [userid] =>1 [date] =>2011-11-10 1:00:00) 
[2] => Array ( [id] =>11 [clientfname] =>Amanda [userid] =>1 [date] =>2011-11-10 1:03:00) 
[3] => Array ( [id] =>12 [clientfname] =>Britany [userid] =>2 [date] =>2011-11-10 1:00:00) 
      )

(Employee Calendar)
<< Monday Oct 8 >>
<<  1:00pm  1:30pm  2:00pm  2:30pm  >>
Macy    Lisa    Amanda              
Vanessa     Britany

Any ideas????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2024-12-21 19:58:23

例如:

$user_appointments = array();
foreach( $appointments as $appointment ) {
    $user_appointments[$appointment['clientid']][] = $appointment;
}

$appointments_grid = array();
foreach( $users as $user ) {
   $appointments_grid = array( 'clientfname' => $user['clientfname'], 'appointments' => $user_appointments[$user['userid']] );
}

我是第一个承认可能有更简洁的解决方案的人。也许如果您发布了从哪里获取这些数据,那么那里也可以做一些事情。但这应该有效。

For example:

$user_appointments = array();
foreach( $appointments as $appointment ) {
    $user_appointments[$appointment['clientid']][] = $appointment;
}

$appointments_grid = array();
foreach( $users as $user ) {
   $appointments_grid = array( 'clientfname' => $user['clientfname'], 'appointments' => $user_appointments[$user['userid']] );
}

I'd be the first to admit that there might be a neater solution. Perhaps if you had posted where you got this data from, something could be done there too. But this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文