2014-06-09 64 views
-1

首先我想感謝您提前給予您的幫助。刪除重複的條目並關注最後一條

我有結構像這樣的數組:

array(18) { 
    ["department"]=> 
    string(14) "Administration" 
    ["employee"]=> 
    string(13) "Joe Smith" 
    ["times"]=> 
    string(6) "7:00am" 
    ["timee"]=> 
    string(6) "7:00pm" 
    ["regular"]=> 
    int(8) 
    ["overtime"]=> 
    int(4) 
    ["d_overtime"]=> 
    int(4) 
    ["total"]=> 
    int(12) 
    ["cost"]=> 
    int(28) 
    ["date"]=> 
    string(12) "May 27, 2014" 
    ["curdp"]=> 
    int(28) 
    ["mtddp"]=> 
    int(28) 
    ["rev"]=> 
    string(10) "35073.8900" 
    ["dp2rev"]=> 
    string(5) "0.08%" 
    ["mtd2rev2013"]=> 
    string(4) "-na-" 
    ["mtdpay2rev"]=> 
    string(5) "0.08%" 
    ["budget"]=> 
    string(4) "1.80" 
    ["counter"]=> 
    NULL 
} 

我需要能夠找到具有相同的「日期」值數組中的最後一個項目。例如,我需要在數組中找到所有5月27日的值,並顯示最後一個條目。

再次感謝您。

回答

0

讓輸入數據是在$數據

$resultArr = array(); 
$filteredByDateArr = array(); 

    foreach($data as $entry) 
    {  
if($entry['date']=='May 27, 2014') 
    $resultArr[] = $entry; 

$filteredByDateArr[$entry['date']][] = $entry; 

    } 

    //last entry 
    var_dump($resultArr[count($resultArr)-1]); 
var_dump($filteredByDateArr); 

//This $filteredByDateArr is the array that will give you date respective data. 
//So if you want to find the last entry of say any date $date 

//You can get it by: 

var_dump($filteredByDateArr[$date][count($filteredByDateArr[$date])-1]); 
+0

感謝這樣一個快速的答案。我認爲這會起作用,但是如果我不知道要過濾哪些內容,我將如何使=='2014年5月27日'動態化?該數組由多個不同的日期結果組成。我需要過濾每月的每一天嗎? – Qtpounder

+0

如果只打算使用最後一個值,爲什麼要構建一個數組? –

+0

傑克 - 我展示的部分是數組的一部分。它會有許多不同的日期和時間,最終結果將顯示在屏幕上,如下所示: 日期其他值1其他值2等 – Qtpounder