0

这是循环:

         foreach($event['Event']['user_id'] as $employee){


     for($date; $date <= $end_date; $date = strtotime("+1 day", $date))
     {
         if(strpos($weekdays, date("N", $date)) !== false){
             $real_date = date("Y-m-d", $date);
             $event['Event']['date'] = $real_date;
             $event['Event']['user_id'] = $employee;
             $this->Event->create();
             $this->Event->save($event);
            debug($event['Event']['user_id']);           
         }
     }

    }

*$event['Event']['user_id']* 的内容如下:

array(
(int) 0 => '23',
(int) 1 => '24',
(int) 2 => '27',
(int) 3 => '28')

如果我尝试在 for 循环之前打印每个$employee,我会打印所有四条记录,但 for 循环仅适用于第一条记录。任何帮助深表感谢。

编辑:添加了pr($event)的结果

Array([Event] => Array
    (
        [project_id] => 62
        [user_id] => Array
            (
                [0] => 23
                [1] => 24
                [2] => 27
                [3] => 28
                [4] => 29
            )

        [hours] => 4
        [minutes] => 0
        [assignment] => 
        [material] => 
        [date] => 2013-10-6
        [start_time] => Array
            (
                [hour] => 09
                [min] => 00
            )

        [finish_time] => Array
            (
                [hour] => 13
                [min] => 00
            )

        [repeat] => 0
        [finish_date] => 
        [weekdays] => 
    )

)

4

1 回答 1

1

让我们看看,在第一个循环中for,你这样做

 for($date; $date <= $end_date; $date = strtotime("+1 day", $date))
 {
     if(strpos($weekdays, date("N", $date)) !== false){
         $real_date = date("Y-m-d", $date);
         $event['Event']['date'] = $real_date;
         $event['Event']['user_id'] = $employee;
         $this->Event->create();
         $this->Event->save($event);
        debug($event['Event']['user_id']);           
     }
 }

$employee特定情况的数组是23(通过您发布的示例)。注意if语句中的特定行。

$event['Event']['user_id'] = $employee;

所以基本上,$event['Event']['user_id']不再是

$event['Event']['user_id'] = array(
(int) 0 => '23',
(int) 1 => '24',
(int) 2 => '27',
(int) 3 => '28')

$event['Event']['user_id'] = 23;

反而。因此,该foreach功能仅适用于第一条记录,然后停止。我不建议更改foreach内部的参数foreach(就像在这种情况下),它总是(或几乎总是)导致混乱。

由于我不知道您要使用此功能做什么,因此我无法给您更多建议,但请重新考虑解决此问题的逻辑。也许像一个简单的变量

$users = $event['Event']['user_id'];
foreach ($users as $employee) {
    /* the rest remains the same*/
}

就足够了。

于 2013-10-17T16:41:19.817 回答