数组中未定义的偏移量错误

时间:2015-11-20 15:34:59

标签: php codeigniter

我有以下代码

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);
}

并且codeigniter仍然显示未定义的偏移量错误,我自己检查了添加到输出数组的值,一切正常,这里缺少什么?或错误在哪里?

1 个答案:

答案 0 :(得分: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;
    }

我试图创造一个&#34;动态&#34;只有必要位置的数组,但这对我有用,如果你们有任何想法做我想做的事情会很棒!

感谢。