2017-09-06 18 views
-3

基本的に私はforeachを使って配列を作成しようとしています。アレイには各デバイスのデータ(約10000個のデバイス)が含まれていますが、一部はデータが古いため空です。間違ったソフトウェアがあれば$ newFaultが作成されるように、以下のforeachでこの配列を使用する必要があります。私はスーパースタックして、それを動作させることはできません。PHP foreachの配列

手がかりはありますか?

私のアレイは、以下のDeviceLogの関係を使用できる必要があります。

 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は配列によってforeachされる必要があるものです。ループ内のすべてのif文は、配列自身をforeachする必要があります。 はい、私はその部分を認識しています。それは基本的に同じものに対して異なる名前を使用する2つの別個の関係です。 – 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(); 
    }