【问题标题】:Undefined offset in custom codeigniter library, what am i doing wrong?自定义codeigniter库中未定义的偏移量,我做错了什么?
【发布时间】:2015-03-21 06:56:51
【问题描述】:

我将在我的代码中查看 php 错误,这会导致 $time 和 $date 数组出现两个错误。 Php 说 Message: Undefined offset: 1 and 2 for both $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

文件名:libraries/Sweph.php

行号:61

遇到了 PHP 错误

严重性:通知

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

文件名:libraries/Sweph.php

行号:61

遇到了 PHP 错误

严重性:通知

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

文件名:libraries/Sweph.php

行号:61

遇到了 PHP 错误

严重性:通知

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

文件名:libraries/Sweph.php

行号:61

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

【问题讨论】:

  • 哪个文件在视图或控制器内部导致错误。写完整的错误消息。
  • 更新了问题,codeigniter 的自定义库。
  • 如果你的两个数组都有值,它不应该产生错误。很难产生场景。爆炸后尝试echo $time[2];die();。是否再次产生错误?

标签: php codeigniter undefined


【解决方案1】:

检查其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

【讨论】:

  • 返回结果:Array ( [0] => 05 [1] => 51 [2] => 00 );所以他们的数组不是空的......
  • 你可以试试我添加的其他方式吗?
  • 数组中的数据来自数据库,我忘了说。
  • 我已经测试了你的场景。 3v4l.org/V0fXu。检查它是否与您的最后相同。随时尝试echo,了解您获得的价值。
  • 结果:小时=05 分钟=51 秒=00 年=1984 周一=06 天=23 完全符合它的结果。
【解决方案2】:
 $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,它将产生相同的警告。

【讨论】:

  • 两个数组都产生结果:Array1 ( [0] => 05 [1] => 51 [2] => 00 ) Array2 ( [0] => 1984 [1] => 06 [2 ] => 23)
  • parag tyagi 回答了问题,但我仍然想知道为什么爆炸功能有问题。因为键值...
猜你喜欢
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 2017-07-25
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多