php数组、时间戳和循环
我有以下代码。$results数组的输出如下php数组、时间戳和循环,php,arrays,Php,Arrays,我有以下代码。$results数组的输出如下 [1]=>stdClass对象([ID]=>4429[post\u date\u gmt]=>2015-03-05 11:04:18) 在第二个循环中,我正在计算今天时间和后发日期之间的差异。在IF语句中返回正确的结果,但是为了得到这里,我丢失了原始$results数组的ID。如果$difference$value的jobdates),它就可以使用并正确关联了。谢谢@GeoffAtkins。那么$key就是ID了?@IanButler-没错。您正在使
[1]=>stdClass对象([ID]=>4429[post\u date\u gmt]=>2015-03-05 11:04:18)
在第二个循环中,我正在计算今天时间和后发日期之间的差异。在IF语句中返回正确的结果,但是为了得到这里,我丢失了原始$results
数组的ID。如果$difference<72
因此,问题是-是否有可能在保持ID和post_date_gmt的同时访问IF语句
<?php
global $wpdb;
$results = $wpdb->get_results(
"SELECT ID, post_date_gmt
FROM wp_posts
WHERE post_type = 'job_listing'"
);
print_r($results);
$jobids = array();
$jobdates = array();
foreach($results as $oneitem) {
$jobids[]=$oneitem->ID;
$jobdates[]=$oneitem->post_date_gmt;
}
foreach($jobdates as $value) {
$postdate = $value;
$today = time();
$postdate = strtotime($postdate);
//echo $postdate;
$difference = round(abs($today-$postdate)/60/60);
if($difference < 72) {
echo $difference;
//change a value in the database where the id matches the id from the $results array
}
}
?>
构建阵列时,为什么不使用作业ID作为jobdates阵列的键
foreach($results as $oneitem) {
$jobdates[$oneitem->ID]=$oneitem->post_date_gmt;
}
然后,当您为第二个foreach循环调用该数组时,也要调用键foreach($key=>$value的jobdates)
,它就可以使用并正确关联了。谢谢@GeoffAtkins。那么$key就是ID了?@IanButler-没错。您正在使用数组键来携带ID。请注意,这仅在ID都是唯一的情况下有效,因为数组的键必须是唯一的。如果您有重复的ID,那么您需要创建一个多维数组来携带ID和作业日期。