2017-09-06 75 views
-3

基本上我只是试图用一个foreach的数组工作。该阵列包含来自每个设备的数据(大约10000个设备),其中一些由于数据较旧而为空。我需要在下面的foreach中使用这个数组来确保$ newFault被创建,如果它有错误的软件的话。我超级卡住,不能得到它的工作。在PHP中的数组foreach

任何线索?

请注意,我的阵列必须能够使用下面的DeviceLogs的关系。

 public function createViaCameras() 
{ 
    set_time_limit(120); 


    //This part removes the entire existing FirmwareFault database for the new info to go in. 
    FirmwareFault::truncate(); 
    CameraUnlinked::truncate(); 

    //This part imports all cameras from today. 
    $today = (new \DateTime); 
    $today->modify('-3 day'); 
    $tomorrow = (new \DateTime); 
    $tomorrow->modify('+1 day'); 

    $devices = Device::take(10)->get(); 
     foreach ($devices as $device) { 


    $logs[$device->id] = DeviceLog::whereNotIn('model', ['Test', 'Test2'])->whereDeviceId($device->id)->whereBetween('created_at', [$today, $tomorrow])->orderBy('created_at', 'desc')->first(); 

} 


    foreach ($logs as $log) { 

     if (count($log->camera)) { 
      if ($log->model = $log->camera->name) { 
       if ($log->acaps == '1' && $log->version !== $log->camera->fwts) { 
         $newFwFault = new FirmwareFault(); 
         $newFwFault->serial = $log->device->serial; 
         $newFwFault->fw = $log->version; 
         $newFwFault->model = $log->model; 
         $newFwFault->new_fw = $log->camera->fwts; 
         $newFwFault->fault_id = $log->acaps; 
         $newFwFault->save(); 
        } elseif ($log->acaps == '2' && $log->version !== $log->camera->fwtb) { 
         $newFwFault = new FirmwareFault(); 
         $newFwFault->serial = $log->device->serial; 
         $newFwFault->fw = $log->version; 
         $newFwFault->model = $log->model; 
         $newFwFault->new_fw = $log->camera->fwdt; 
         $newFwFault->fault_id = $log->acaps; 
         $newFwFault->save(); 
        } elseif ($log->acaps == '3' && $log->version !== $log->camera->fwppt) { 
         $newFwFault = new FirmwareFault(); 
         $newFwFault->serial = $log->device->serial; 
         $newFwFault->fw = $log->version; 
         $newFwFault->model = $log->model; 
         $newFwFault->new_fw = $log->camera->fwppt; 
         $newFwFault->fault_id = $log->acaps; 
         $newFwFault->save(); 
        } elseif ($log->acaps == '4' && $log->version !== $log->camera->fw) { 
         $newFwFault = new FirmwareFault(); 
         $newFwFault->serial = $log->device->serial; 
         $newFwFault->fw = $log->version; 
         $newFwFault->model = $log->model; 
         $newFwFault->new_fw = $log->camera->fw; 
         $newFwFault->fault_id = $log->acaps; 
         $newFwFault->save(); 
        } elseif ($log->acaps == '0' && $log->version !== $log->camera->fw) { 
         $newFwFault = new FirmwareFault(); 
         $newFwFault->serial = $log->device->serial; 
         $newFwFault->fw = $log->version; 
         $newFwFault->model = $log->model; 
         $newFwFault->new_fw = $log->camera->fw; 
         $newFwFault->fault_id = $log->acaps; 
         $newFwFault->save(); 
        } else { 
         $newUnlink = new CameraUnlinked(); 
         $newUnlink->serial = $log->device->serial; 
         $newUnlink->model = $log->model; 
         $newUnlink->save(); 
        } 
      } else { 
      $newUnlink = new CameraUnlinked(); 
      $newUnlink->serial = $log->device->serial; 
      $newUnlink->model = $log->model; 
      $newUnlink->save(); 
      } 
     } else { 
      $newUnlink = new CameraUnlinked(); 
      $newUnlink->serial = $log->device->serial; 
      $newUnlink->model = $log->model; 
      $newUnlink->save(); 
     }; 

    }; 

    return redirect()->back(); 
} 
+0

您的'$ newFault'数组在哪里?你的意思是'$ newFwFault'? – hassan

+1

也是,你知道这个'$ log-> model = $ log-> camera-> name'? – hassan

+0

是的,$ newFwFault是需要由数组进行处理的那一个。循环中的所有if语句都需要对数组本身进行foreach。 是的,我知道那一部分。这是两个单独的关系,基本上对同一事物使用不同的名称。 – Stoff

回答

0

//我试图做一些重构,但很难理解你想达到什么,或者你的问题是什么。我清理了一下你的代码并添加了一些评论,我希望这会帮助你确定哪里出了问题。我重构了你的循环,但是(没有任何方法去测试它),我相信它的实际结果和以前一样。

public function createViaCameras() 
    { 
     set_time_limit(120); 

     //This part removes the entire existing FirmwareFault database for the new info to go in. 
     FirmwareFault::truncate(); 
     CameraUnlinked::truncate(); 

     //This part imports all cameras from today. 
     $threeDaysAgo = (new \DateTime)->modify('-3 day'); 
     $tomorrow  = (new \DateTime)->modify('+1 day'); 

     $devices = Device::take(10)->get(); // Why only 10? You also might want to use limit and offset instead, they are closer to the actual query language 
     $logs = [];       // Instantiate first. If the following loop doesn't find any logs, your $logs loop will fail. 
     foreach ($devices as $device) { 
      $logs[$device->id] = DeviceLog::whereNotIn('model', ['Test', 'Test2']) 
       ->whereDeviceId($device->id) 
       ->whereBetween('created_at', [$threeDaysAgo, $tomorrow]) // A log that was created tomorrow??? 
       ->orderBy('created_at', 'desc') 
       ->first();            // Why only the first log? 
     } 

     foreach ($logs as $log) { 

      if (
       !count($log->camera) ||    // $log->camera is an object. Use another function to do the check that you want. What are you checking for`? 
       $log->model !== $log->camera->name 
      ) { 
       $model = new CameraUnlinked(); 
       continue; 
      } 

      $condition1 = $log->acaps === '1' && $log->version !== $log->camera->fwts; 
      $condition2 = $log->acaps === '2' && $log->version !== $log->camera->fwtb; 
      $condition3 = $log->acaps === '3' && $log->version !== $log->camera->fwppt; 
      $condition4 = $log->acaps === '4' && $log->version !== $log->camera->fw; 
      $condition5 = $log->acaps === '0' && $log->version !== $log->camera->fw; 

      if ($condition1 || $condition2 || $condition3 || $condition4 || $condition5) { 
       $model = new FirmwareFault(); 
       $model->fw = $log->version; 
       $model->fault_id = $log->acaps; 

       if ($condition1) { 
        $model->new_fw = $log->camera->fwts; 
       } elseif ($condition2) { 
        $model->new_fw = $log->camera->fwdt; 
       } elseif ($condition3) { 
        $model->new_fw = $log->camera->fwppt; 
       } elseif ($condition4) { 
        $model->new_fw = $log->camera->fw; 
       } elseif ($condition5) { 
        $model->new_fw = $log->camera->fw; 
       } 
      } else { 
       $model = new CameraUnlinked(); 
      } 

      $model->serial = $log->device->serial; 
      $model->model = $log->model; 
      $model->save(); 
     }; 

     return redirect()->back(); 
    }