PHP访问存储在数组中的对象
我对php采用面向对象的方法还不熟悉……实际上,总体来说,我有很多要学习的东西,但唯一的学习方法是通过实践 所以我有一个数组,它保存了几个DateTime对象。我知道这是真的,因为我在数组上运行了var_dump,它确实保存了几个对象 我需要检查每一个日期,确保没有一天的差异超过一天 我的研究似乎表明,我们无法使用下标运算符访问或修改对象:PHP访问存储在数组中的对象,php,Php,我对php采用面向对象的方法还不熟悉……实际上,总体来说,我有很多要学习的东西,但唯一的学习方法是通过实践 所以我有一个数组,它保存了几个DateTime对象。我知道这是真的,因为我在数组上运行了var_dump,它确实保存了几个对象 我需要检查每一个日期,确保没有一天的差异超过一天 我的研究似乎表明,我们无法使用下标运算符访问或修改对象: $foo = $neat_array[$i+1]->format('U') ; //looking to format DateTime object
$foo = $neat_array[$i+1]->format('U') ; //looking to format DateTime object as unix
//this returns an error every time
好的,我对此很满意,但我无法理解访问数组中特定项的语法,因此它被视为一个对象而不会产生错误
我已经拼凑出使用->是我需要做的,但我从来没有得到任何有用的结果
这是我试图做的伪代码
foreach($date_array as $date)
{
//check to see if the difference between the next date in the array and the current date of the array is greater than one day.
//I cannot use diff because I am on php 5.2 so I am trying this
$date->format('U') and then doing the math
}
因为在实验中学习(失败)是一种很好的学习方式,这里有一些东西可以让你开始学习
$date_array = array(
new DateTime("tomorrow"),
new DateTime("now"),
new DateTime("yesterday"),
new DateTime("last day of october")
);
for( $i = 0, $count = count( $date_array); $i < $count; $i += 2) {
// Get the current object AND the next one in the array
echo $date_array[ $i ]->format('U'); echo '<br />';
echo $date_array[ $i + 1 ]->format('U'); echo '<br />';
// Now that you have the UNIX timestamps, you can do the math you need in here.
}
$date\u数组=数组(
新日期时间(“明天”),
新日期时间(“现在”),
新日期时间(“昨天”),
新日期时间(“10月最后一天”)
);
对于($i=0,$count=count($date_数组);$i<$count;$i+=2){
//获取数组中的当前对象和下一个对象
echo$date_数组[$i]->格式('U');echo'
';
echo$date_数组[$i+1]->格式('U');echo'
';
//现在您已经有了UNIX时间戳,可以在这里进行所需的计算了。
}
请注意,如果数组包含奇数个元素,则上述操作将失败-我将把该修复留给您(如果您需要的话)
编辑:您可能会因为以下两个原因之一而出现此错误:
$session\u dates=$date\u array=array(
新日期时间(“明天”),
新日期时间(“昨天”),
新日期时间(“现在”),
);
对于($i=0,$count=count($date\u数组)-1;$i<$count;$i++)
{
if($date\U数组[$i+1]->格式($U')-$date\U数组[$i]->格式($U'))>86400)
{
骰子('日期数组不包含连续日期');
}
}
好的……首先,我感谢大家的帮助。Nickb,谢谢你的帮助和演示。正如我先前推测的那样,您建议我可能尝试访问数组中不存在的元素,这是绝对正确的。我在这里要做的是我正在创建的网站的一个非常重要的部分。这整个混乱将作为检查预订日期的类中的一个函数 这是我第一次尝试堆栈溢出,所以请原谅我,但我可能应该在我的初始问题中提供更多信息,但我确信我知道代码不起作用的原因……这是完全错误的 看,因为我总是需要知道当前元素,加上数组中的下一个元素,所以我的代码总是在循环的最后一次运行时中断,因为它试图访问当前键,再加上下一个键(上次尝试时不存在) 现在,真正令人沮丧的是,我整晚都在为这段代码使用for循环,但我仍然无法让它在不中断的情况下干净地遍历每一项……我总是在数组的最后一项上运行过度,并找出错误。我认为问题一直是,我不需要遍历数组中的每一项,只需要遍历倒数第二项(加上检查下一项),然后退出循环 这是我的代码,终于可以工作了,但我真的不喜欢它…我感觉在语法和其他方面有一些简单的修正,这将使它更有效…如果没有,就这样吧…顺便说一下,基本上我的测试代码充满了回声,以查看实际发生了什么,但它最终做的一切都是正确的:
$count = (count($session_dates)-1);
//never could get that last item to not trigger error, so subtracting one from the count was the only thing to end my frustration!!!but as I write this answer I think I see why and that is because we don't need to go through each item of the array, just each but the last item
echo '<p>'.$count.' this is the count of session dates </p>';
for ($i=0; $i<$count; $i++){
$unix_one = $session_dates[$i+1]->format('U');
//make some variables that are in the date format I want to visualize for echoing
$unix_echo_one = $session_dates[$i+1]->format("n".'/'."j".'/'."Y");
$unix_two = $session_dates[$i]->format('U');
$unix_echo_two = $session_dates[$i]->format("n".'/'."j".'/'."Y");
//see if the difference is greater than one day
if(($unix_one-$unix_two)/86400>1){
echo 'these dates are absolutely not consecutive, you fail!!';
//we only need one occurrence to wreck what we are trying to do, so just exit if it happens.
exit();
}
//I needed to see if any of the loop was happening to help try and figure this out!!
echo '<p>'.$unix_echo_one.'||'.$i.'||'.$unix_echo_two.'</p>';
}
$count=(count($session\u dates)-1);
//永远无法让最后一项不触发错误,所以从计数中减去一项是结束我沮丧的唯一方法!!!但当我写下这个答案时,我想我明白了原因,那是因为我们不需要遍历数组中的每一项,只需要遍历最后一项
回显“”.$count.。这是会话日期的计数;
对于($i=0;$iformat('U');
//制作一些我想要可视化的日期格式的变量,以便进行回音
$unix_echo_one=$session_dates[$i+1]->格式(“n”/“.j”/“.Y”);
$unix_two=$session_日期[$i]->格式('U');
$unix_echo_two=$session_dates[$i]->格式(“n“/”j“/”Y”);
//看看差异是否大于一天
如果($unix_one-$unix_two)/86400>1){
echo“这些日期绝对不是连续的,你失败了!!”;
//我们只需要一次事件就可以破坏我们正在尝试做的事情,所以如果它发生了就退出。
退出();
}
//我需要看看是否有任何循环发生,以帮助尝试和解决这个问题!!
回显“”.$unix_echo_one.“| |”.$i.| |”.$unix_echo_two.“”;
}
您的第一个示例产生的错误是什么?@absentx-您的第一个示例不应该在PHP5.2中产生错误(我刚刚测试过)。调用成员函数格式()在第121行的/class_library.php中的非对象上,我的代码非常相似,但在非对象上调用函数格式时仍然会出现致命错误。嗯,我应该先发布我的实际代码…现在我该怎么做…第一次在Stack上我更新了我的答案,并给出了可能的解释-您可能正在访问a中的一个元素不存在的rray(通过超过arr
$count = (count($session_dates)-1);
//never could get that last item to not trigger error, so subtracting one from the count was the only thing to end my frustration!!!but as I write this answer I think I see why and that is because we don't need to go through each item of the array, just each but the last item
echo '<p>'.$count.' this is the count of session dates </p>';
for ($i=0; $i<$count; $i++){
$unix_one = $session_dates[$i+1]->format('U');
//make some variables that are in the date format I want to visualize for echoing
$unix_echo_one = $session_dates[$i+1]->format("n".'/'."j".'/'."Y");
$unix_two = $session_dates[$i]->format('U');
$unix_echo_two = $session_dates[$i]->format("n".'/'."j".'/'."Y");
//see if the difference is greater than one day
if(($unix_one-$unix_two)/86400>1){
echo 'these dates are absolutely not consecutive, you fail!!';
//we only need one occurrence to wreck what we are trying to do, so just exit if it happens.
exit();
}
//I needed to see if any of the loop was happening to help try and figure this out!!
echo '<p>'.$unix_echo_one.'||'.$i.'||'.$unix_echo_two.'</p>';
}