自定义代码点火器库中未定义的偏移量,我做错了什么


Undefined offset in custom codeigniter library, what am i doing wrong?

我将在我的代码中查看php错误,这会导致$time和$date数组出现两个错误。Php 说消息:未定义的偏移量:1 和 2 表示 $time 和 $date。

所以在我完成 explode() 之后的键是未定义的吗?这究竟是怎么回事,为什么会这样?我该如何解决这个问题?数组显示的结果来自我在使用 print_r 时可以看到的数据库。

 Array ( [0] => 05 [1] => 51 [2] => 00 ) 
 Array ( [0] => 1984 [1] => 06 [2] => 23 )

我尝试了几件事,似乎都没有奏效。我不记得我以前有过这个问题...我几乎会开始想,也许这个问题可能与Codeigniter有关?

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sweph {
	
	public $julian_ut = null;	
	
	public $config = array();
	
	public $chart_id='';
	
	public $user_id='';
	
	public $chart_name='';
	
	public $chart_time='';
	
	public $chart_date='';
	
	public $chart_search='';
	
	public $chart_timezone='';
	
	public $chart_houses='';
	
	public $chart_longitude='';
	
	public $chart_latitude='';
	
	public $time = array();
	
	public $date = array();
	
	/***
	 * Initialize chart details
	 **/
	public function __construct($config=array()) {
		// Get instance 
		$this->CI =& get_instance();  
		
		// Initializes and Loads Ephemeris files from Directory
		swe_set_ephe_path(FCPATH.'ephemeris');
	
		// Creates objects from config array
		foreach ($config as $property => $value)
                {
                     $this->$property = $value;
                } 
		
		// Create time and date array
		$time = explode(":", $this->chart_time);
		$date = explode("-", $this->chart_date);
		
		// Calculates Julian day 
		$this->julian_ut = swe_julday(intval($date[0]), $date[1], $date[2], ($time[0] + $time[1] / 60 + $time[2] / 3600) , SE_GREG_CAL);	
		
        }
	/***
	 * Return time/day in julian day number
	 **/	
	public function get_julian_ut() {
		return $this->julian_ut;
	}
}

**错误**

遇到 PHP 错误

严重性:通知

消息:未定义偏移量:1

文件名: 库/瑞典.php

行号:61

遇到 PHP 错误

严重性:通知

消息:未定义的偏移量:2

文件名: 库/瑞典.php

行号:61

遇到 PHP 错误

严重性:通知

消息:未定义偏移量:1

文件名: 库/瑞典.php

行号:61

遇到 PHP 错误

严重性:通知

消息:未定义的偏移量:2

文件名: 库/瑞典.php

行号:61

更新:忘记提及数组具有来自数据库的结果并添加了错误。

检查其var_dump(),然后相应地使用它进行索引

$time = explode(":", $this->chart_time);
var_dump($time);
$date = explode("-", $this->chart_date);
var_dump($date);

但这不是获取日期和时间的日期、月份或时间的正确方法。


推荐:

<?php
$date = '2015-01-22';
$time = '18:16:12';
$dt = new DateTime($date);
$tm = new DateTime($time);
$day = $dt->format('d');
$month = $dt->format('m');
$year = $dt->format('Y');
$hour = $tm->format('H');
$min = $tm->format('i');
$sec = $tm->format('s');


演示:
http://3v4l.org/VDB7l


文档:
http://docs.php.net/class.datetime

 $time = explode(":", $this->chart_time);
 //it seems $time does not have value for 1,2 index 
 //$time[1] $time[2] does not exists

这就是你收到警告的原因。

 $date = explode("-", $this->chart_date);

确保$time至少有 3 个尺寸。如果大小小于 3,它将产生相同的警告。