2015-11-20 75 views
-2

偏移誤差,我有以下代碼未定義數組

public function testcharts() 
{ 
    //Current session token 
    $token = $this->session->userdata()['token']; 

    //Begin of the API request 
    $service_url = 'http://api.hubapi.com/deals/v1/deal/recent/created?access_token='.$token.'&count=500'; 
    $curl = curl_init($service_url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
    $curl_response = curl_exec($curl); 
    if ($curl_response === false) 
    { 
     //If the request fail, get the error and the number of the error 
     $error = curl_error($curl); 
     $errorNumber = curl_errno($curl); 
     curl_close($curl); 
     die('An error occurred during the request execution. Additional info: <br>Error: ' .$error.' <br>Error #: '.$errorNumber); 
    }  
    curl_close($curl); 

    //Putting the response into an array 
    $decoded = json_decode($curl_response,true); 
    if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') 
    { 
     die('error occurred: ' . $decoded->response->errormessage); 
    } 

    $created = array(); 

    //Array than will store all the deals filtered by stage (closed won, finished & contract payed) 
    $cr = 0; //Aux counter to the previous array 

    for($i=0;$i<count($decoded['results']);$i++) 
    {   
     if(isset($decoded['results'][$i]['properties']['createdate']['value'])) 
     { 
      $createdate = $decoded['results'][$i]['properties']['createdate']['value']; 
     } 
     //Filtering the deals by stage and close date month 
     if(isset($createdate) && date('m')==date('m',$createdate/1000)) 
     { 
      //Putting the results into 'deals' array 
      $created[$cr]=$decoded['results'][$i]['properties'];   
      $cr++;   
     }    
    }  

    function dealsPerDay($deals_array) 
    { 
     $month_name = date('F'); //e.g. January 
     $month = date('m'); //e.g. 1 
     $year = date('Y'); //e.g. 2015 
     $days_in_month = date('t'); //The amount of days in the month e.g. January has 31 days 
     $current_day = date('j'); //The numeric value of the current date of the month e.g. January 14th = 14 
     $output = array(); 

     for ($i=1; $i <= $days_in_month; $i++) { 
      $output[$i]=0; 
     }   

     for ($j=1; $j <= $days_in_month ; $j++) 
     { 
      for ($i=0; $i < count($deals_array); $i++) 
      {   
       if(date('j',$deals_array[$i]['createdate']['value']/1000)==$j) 
       {      
        $output[$j]++; 
       } 
      } 
     } 

     return $output; 
    } 

    $data['dealsPerDay'] = dealsPerDay($created); 
    $data['daysInMonth'] = date('t'); 
    $this->load->view('dashboard/test-linechart',$data); 
} 

而笨仍然顯示一個未定義的偏移誤差,我檢查了我自己添加到輸出數組中的值,並一切正常,缺什麼這裏?或者錯誤在哪裏?

+0

你可以做一個var_dump($ deals_array)並把它放在你的文章 –

+0

如果你要顯示$ deals_array的樣子,它會更容易找出問題所在。另外,更詳細的錯誤描述會有所幫助 – Marius

+0

如果您提供了足夠的代碼,助手可以運行並重現問題,您將得到更好的迴應。請參閱http://stackoverflow.com/help/mcve。 –

回答

0

該解決方案設定輸出到一個空白陣列

function dealsPerDay($deals_array) 
    { 
     $month_name = date('F'); //e.g. January 
     $month = date('m'); //e.g. 1 
     $year = date('Y'); //e.g. 2015 
     $days_in_month = date('t'); //The amount of days in the month e.g. January has 31 days 
     $current_day = date('j'); //The numeric value of the current date of the month e.g. January 14th = 14 
     $output = array(); 

     for ($i=1; $i <= $days_in_month; $i++) { 
      $output[$i]=0; 
     }   

     for ($j=1; $j <= $days_in_month ; $j++) 
     { 
      for ($i=0; $i < count($deals_array); $i++) 
      {   
       if(date('j',$deals_array[$i]['createdate']['value']/1000)==$j) 
       {      
        $output[$j]++; 
       } 
      } 
     } 

     return $output; 
    } 

我試圖創建一個只必要位置的「動態」的數組,但這個工作對我來說,如果您有任何的一個想法,做我想做的事情會很棒!

謝謝。