2015-11-20 67 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; 
    } 

我试图创建一个只必要位置的“动态”的数组,但这个工作对我来说,如果您有任何的一个想法,做我想做的事情会很棒!

谢谢。