PHP范围问题

PHP范围问题,php,arrays,scope,Php,Arrays,Scope,我试图查看一个记录数组(员工),在这个循环中,我调用一个函数返回另一个记录数组(每个员工的约会) 这是正常的,但是,在脚本的后面,我需要再次循环记录,这次使用约会数组,但是它们不可用 foreach ($staffmembers as $staffmember) { //do some other stuff //print_r($staffm

我试图查看一个记录数组(员工),在这个循环中,我调用一个函数返回另一个记录数组(每个员工的约会)

这是正常的,但是,在脚本的后面,我需要再次循环记录,这次使用约会数组,但是它们不可用

foreach ($staffmembers as $staffmember)
{                                                             
        //do some other stuff
        //print_r($staffmember['appointments'] no longer does anything
}
通常,我会在第二个循环中从第一个循环执行函数,但是这个循环已经嵌套在另外两个循环中,这将导致相同的sql查询运行168次

有人能提出解决办法吗

如有任何建议,将不胜感激

感谢

迭代数组的副本。如果要更改该值,需要执行以下操作:


foreach($staffmembers as&$staffmember)//我假设第二个循环与第一个循环发生在同一个函数中?我不确定我是否理解。循环没有自己的作用域。你在哪一点上损失了
$staffmembers
,为什么?我认为这与他的问题无关。编辑:好的,我想这就是他的意思,不是一个真正的范围问题,真的。@artifact:它在某种程度上与范围有关。副本在循环外部不可见;)好。。。
$staffmember
的最后一个副本将在循环外部可见,因此它实际上不是一个范围问题…=)
foreach ($staffmembers as $staffmember)
{                                                             
        //do some other stuff
        //print_r($staffmember['appointments'] no longer does anything
}
foreach($staffmembers as &$staffmember) // <-- note the &
{   
    $staffmember['appointments'] = get_staffmember_appointments_for_day($staffmember);
    //  print_r($staffmember['appointments'] works fine 
}