【问题标题】:Add missing dates in array在数组中添加缺失的日期
【发布时间】:2022-03-30 11:04:29
【问题描述】:

我想显示本周的数据。在数据库中,我有一些特定的日期数据,我已经填写了缺失的日期,在视图页面中,我通过 foreach 循环显示了这些数据,因为我想为它们设置不同的值。但由于 2 个 foreach 循环,它显示了多次数据。我想在一个数组中得到结果。我的代码是:

 public function cashflowdetails() { 
             
            $cashflow = CashModel::orderBy('payment_date','asc') ->get();  
            $cashflow_array = [];
            $cashflowDate_array = [];
                 
            $now = Carbon::now();
            if (Input::get('from') != '') {
                $weekStartDate =Input::get('from') ;
            }
            else{
                $weekStartDate = Carbon::now()->startOfWeek();
            }  
            if (Input::get('to') != '') {
                $weekEndDate = Input::get('to');
            }
            else{
                $weekEndDate = Carbon::now()->endOfWeek()->addDay();
            } 
                $period = CarbonPeriod::create($weekStartDate, $weekEndDate);
                foreach ($period as $date) {
                    $dt    = new DateTime($date->format("Y-m-d")); 

                    if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
                    {
                        foreach($cashflow as $cash){ 
                                if($date->format("Y-m-d")  === $cash->payment_date )
                                {    
                                    $dbData['id'] = $cash->id;
                                    $dbData['payment_date'] = $cash->payment_date;
                                    $d    = new DateTime($cash->payment_date); 
                                    $dbData['day'] = $d->format('l');
                                    $dbData['opening_balance'] = $cash->opening_balance;
                                    $dbData['other_income'] = $cash->other_income;
                                    $dbData['rent'] = $cash->rent;
                                    $dbData['labour'] = $cash->labour;
                                    $dbData['electricity'] = $cash->electricity;
                                    $dbData['creditors'] = $cash->creditors; 
                                    $dbData['insurance'] = $cash->insurance;
                                    $dbData['direct_debits'] = $cash->direct_debits;
                                    $dbData['others'] = $cash->others; 
                                    $dbData['total_amount'] = $cash->total_amount; 
                                    $dbData['updated_at'] = $cash->updated_at; 
                                    $dbData['created_at'] = $cash->created_at;  
                                    print_r($cash->payment_date);   
                                    $cashflow_array[] = $dbData;                                                         
                                }
                                else{ 
                                    $dbData['id'] = null;
                                    $dbData['payment_date'] = $date->format("Y-m-d");
                                    $d    = new DateTime($date->format("Y-m-d")); 
                                    $dbData['day'] = $d->format('l');
                                    $dbData['labour'] = 0;
                                    $dbData['opening_balance'] = 0;
                                    $dbData['other_income'] = 0;
                                    $dbData['rent'] =0;
                                    $dbData['electricity'] = 0;
                                    $dbData['creditors'] = 0; 
                                    $dbData['insurance'] =0;
                                    $dbData['direct_debits'] = 0;
                                    $dbData['others'] = 0; 
                                    $dbData['total_amount'] = 0; 
                                    $dbData['updated_at'] = ''; 
                                    $dbData['created_at'] = ''; 
                                    $cashflowDate_array[] = $dbData;
                                }  
                                //break;     
                            }
                                
                        }
                    }  
                 $cashflow = array_replace($cashflowDate_array, $cashflow_array); 
                return $cashflow; 
    } 

【问题讨论】:

    标签: php mysql arrays laravel


    【解决方案1】:
    public function cashflowdetails() { 
                 
        $cashflow = CashModel::orderBy('payment_date','asc') ->get();  
        $cashflow_array = [];
        $cashflowDate_array = [];
             
        $now = Carbon::now();
        
        if (Input::get('from') != '') {
            $weekStartDate =Input::get('from') ;
        }
        else{
            $weekStartDate = Carbon::now()->startOfWeek();
        }  
        if (Input::get('to') != '') {
            $weekEndDate = Input::get('to');
        }
        else{
            $weekEndDate = Carbon::now()->endOfWeek()->addDay();
        } 
        
        $period = CarbonPeriod::create($weekStartDate, $weekEndDate);
        foreach ($period as $date)
        {
            //add a flag to see if the code was executed or not
            $isExecuted = false;
            $dt    = new DateTime($date->format("Y-m-d")); 
            if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
            {
                foreach($cashflow as $cash){ 
                    if(  $date->format("Y-m-d")   === $cash->payment_date )
                    {    
                        $dbData['id'] = $cash->id;
                        $dbData['payment_date'] = $cash->payment_date;
                        $d    = new DateTime($cash->payment_date); 
                        $dbData['day'] = $d->format('l');
                        $dbData['opening_balance'] = $cash->opening_balance;
                        $dbData['other_income'] = $cash->other_income;
                        $dbData['rent'] = $cash->rent;
                        $dbData['labour'] = $cash->labour;
                        $dbData['electricity'] = $cash->electricity;
                        $dbData['creditors'] = $cash->creditors; 
                        $dbData['insurance'] = $cash->insurance;
                        $dbData['direct_debits'] = $cash->direct_debits;
                        $dbData['others'] = $cash->others; 
                        $dbData['total_amount'] = $cash->total_amount; 
                        $dbData['updated_at'] = $cash->updated_at; 
                        $dbData['created_at'] = $cash->created_at;
    
                        //if executed, assign it true 
                        $isExecuted = true; 
                        //psuh the values to array
                        $cashflow_array[] = $dbData;
                        //break the loop
                        break;                          
                    }               
    
                }     
                //if not found in database
                if(!$isExecuted){               
                    $dbData['id'] = NULL;
                    $dbData['payment_date'] = $date->format("Y-m-d");
                    $d    = new DateTime($date->format("Y-m-d")); 
                    $dbData['day'] = $d->format('l');
                    $dbData['labour'] = 0;
                    $dbData['opening_balance'] = 0;
                    $dbData['other_income'] = 0;
                    $dbData['rent'] =0;
                    $dbData['electricity'] = 0;
                    $dbData['creditors'] = 0; 
                    $dbData['insurance'] =0;
                    $dbData['direct_debits'] = 0;
                    $dbData['others'] = 0; 
                    $dbData['total_amount'] = 0; 
                    $dbData['updated_at'] = ''; 
                    $dbData['created_at'] = ''; 
                    $cashflow_array[] = $dbData;                          
                }
            }
        } 
    
        return $cashflow_array; 
        
    }
    

    【讨论】:

      【解决方案2】:
      you can achieve it easily by using a variable storing your current date, and double checking if the date is repeating or not.
      For example:
      //all the below code goes inside the foreach($cashflow as $cash)
      
      //declare variable
      
      
          $flag = false;  
          $counter = 0;
       
          if($counter == 0)
          {
              $dt = new DateTime($date->format("Y-m-d"));
              $curDate = $dt;
          }else{
                    $dt = new DateTime($date->format("Y-m-d"));
               }  
          
          if($curDate == $dt && counter !=0)
          {
             $flag = true;
          }  
      
          $counter++;
      
      //on your if conditions if($dt->format('l')!= 'Sunday' && $dt->format('l')!= 'SUNDAY' && $dt->format('l')!= 'SATURDAY' && $dt->format('l')!= 'Saturday')
      
      
      //please add the condition:
          && flag == false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        相关资源
        最近更新 更多