2015-05-28 64 views
0

我有以下代碼。像這樣php數組,時間戳和循環

[1] => stdClass Object ([ID] => 4429 [post_date_gmt] => 2015-03-05 11:04:18)

的$結果陣列輸出在第二循環我今天的計算時間和所述踵之間的差異。在IF語句中返回正確的結果,但是,到了這裏,我已經從原始$results陣列中丟失了ID。我需要改變基於ID的數據庫中的值如果$difference < 72

所以問題是 - 是否有可能到達IF語句,同時保持ID和post_date_gmt在一起?

<?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 
    } 
} 
?> 

感謝您的任何建議!

回答

0

在構建陣列時,爲什麼不使用作業ID作爲jobdates數組的關鍵字?

foreach($results as $oneitem) { 
    $jobdates[$oneitem->ID]=$oneitem->post_date_gmt; 
} 

然後當你調用數組第二foreach循環,撥打鍵以及foreach($jobdates as $key => $value),這將是提供給使用和正確關聯。

+0

謝謝@GeoffAtkins。那麼$ key會成爲ID嗎? –

+0

@IanButler - 是的。您正在使用數組密鑰來攜帶該ID。只是一個警告的話,這隻會在ID是唯一的時候才起作用,因爲數組的鍵必須是唯一的。如果您有重複的ID,那麼您需要創建一個多維數組,以將ID和作業日期一起攜帶。 –