2011-09-06 26 views
-1

我嘗試合併來自多個查詢的結果並將它們放入數組中。我發現存在一些重複。我想要做的是保留第一條記錄,並在存在重複時刪除以下相同的記錄。我想寫一個函數來過濾數組並刪除重複。任何人都可以幫助我嗎?謝謝如何消除像這樣的多維數組中的重複問題(PHP)

array(4) { 
    [0]=> 
    array(14) { 
    ["user_id"]=> 
    string(7) "2620613" 
    ["first_name"]=> 
    string(5) "Susan" 
    ["last_name"]=> 
    string(9) "Alexander" 
    ["client"]=> 
    string(7) "Pro" 
    ["ssn"]=> 
    string(9) "4234324" 
    ["hiredate"]=> 
    string(10) "2008-04-11" 
    ["hra_id"]=> 
    string(6) "43244" 
    ["hra_date"]=> 
    string(10) "2011-08-17" 
    ["hra_maileddate"]=> 
    NULL 
    ["screening_id"]=> 
    string(6) "3764551" 
    ["screening_date"]=> 
    string(10) "2011-08-12" 
    ["screening_maileddate"]=> 
    NULL 
    ["ref"]=> 
    string(1) "D" 
    ["mailable"]=> 
    string(3) "Yes" 
    } 
    [1]=> 
    array(14) { 
    ["user_id"]=> 
    string(7) "263453" 
    ["first_name"]=> 
    string(6) "Dharti" 
    ["last_name"]=> 
    string(6) "Surani" 
    ["client"]=> 
    string(7) "Pro" 
    ["ssn"]=> 
    string(9) "345325" 
    ["hiredate"]=> 
    string(10) "2002-04-29" 
    ["hra_id"]=> 
    string(6) "3455345" 
    ["hra_date"]=> 
    string(10) "2010-03-22" 
    ["hra_maileddate"]=> 
    NULL 
    ["screening_id"]=> 
    string(6) "234234" 
    ["screening_date"]=> 
    string(10) "2011-07-11" 
    ["screening_maileddate"]=> 
    NULL 
    ["ref"]=> 
    string(1) "D" 
    ["mailable"]=> 
    string(3) "Yes" 
    } 
    [2]=> 
    array(14) { 
    ["user_id"]=> 
    string(7) "2685056" 
    ["first_name"]=> 
    string(4) "Alex" 
    ["last_name"]=> 
    string(2) "Hu" 
    ["client"]=> 
    string(7) "Pro" 
    ["ssn"]=> 
    string(9) "000005562" 
    ["hiredate"]=> 
    string(10) "2011-09-15" 
    ["hra_id"]=> 
    string(6) "528948" 
    ["hra_date"]=> 
    string(10) "2011-07-06" 
    ["hra_maileddate"]=> 
    NULL 
    ["screening_id"]=> 
    string(6) "377382" 
    ["screening_date"]=> 
    string(10) "2011-06-15" 
    ["screening_maileddate"]=> 
    NULL 
    ["ref"]=> 
    string(1) "D" 
    ["mailable"]=> 
    string(3) "Yes" 
    } 
    [3]=> 
    array(14) { 
    ["user_id"]=> 
    string(7) "2685056" 
    ["first_name"]=> 
    string(4) "Alex" 
    ["last_name"]=> 
    string(2) "Hu" 
    ["client"]=> 
    string(7) "Pro" 
    ["ssn"]=> 
    string(9) "000005562" 
    ["hiredate"]=> 
    string(10) "2011-09-15" 
    ["hra_id"]=> 
    string(6) "528948" 
    ["hra_date"]=> 
    string(10) "2011-07-06" 
    ["hra_maileddate"]=> 
    NULL 
    ["screening_id"]=> 
    string(6) "377971" 
    ["screening_date"]=> 
    string(10) "2011-09-13" 
    ["screening_maileddate"]=> 
    NULL 
    ["ref"]=> 
    string(1) "E" 
    ["mailable"]=> 
    string(3) "Yes" 
    } 
} 

我嘗試使用下面的函數來刪除重複,但它不工作得很好,可能會導致在某些情況下的錯誤。

$resultDE = array_merge($resultD1, $resultE1, $resultE2); 
    function specified_array_unique($array) 
    { 
     for($i=0; $i<count($array); $i++){ 
     if(($array[$i]['user_id'] == $array[$i+1]['user_id']) && isset($array[$i+1])) 
     unset($array[$i+1]); 
     } 

     return $array; 
    } 

var_dump(super_unique($resultDE)); 
+0

如果你可以給我們的代碼,你有這麼遠,我們可以幫助您解決問題...... –

+0

您使用的是哪個版本的PHP? –

+0

您需要告訴我們您的「重複」的定義。在你的例子中,沒有一個陣列完全一樣,儘管我們中的一些人在做我們所做的事情時感到驚人,但我們並不是通靈者。 –

回答

1

您需要決定要使用哪個數組鍵來確定記錄是否重複。在以下示例中,您可以將此密鑰傳遞給第二個參數。

編輯我只是做這個更有效率..

function array_remove_duplicates ($array, $key) { 
    $temp = array(); 
    foreach ($array as $k => $record) { 
    if (isset($record[$key])) { 
     // We're only going to filter records that actually have the supplied key present 
     if (in_array($record[$key],$temp)) { 
     // Key already exists, ignore this one 
     unset($array[$k]); 
     } else { 
     // Key doesn't exist, add it to the tracking array 
     $temp[] = $record[$key]; 
     } 
    } 
    } 
    // we have filtered out the records we don't want so re-order the indexes and return the array 
    return array_merge($array); 
} 

// ...and you could use it like this 
$filteredArray = array_remove_duplicates($originalArray,'user_id'); 
+0

令人驚歎。它工作完美。我感謝你的幫助〜! –

+1

@ user921902:如果這是最好的答案,請點擊旁邊的複選標記以「接受」它。 – webbiedave

0

嘗試array_unique($myarray)(見here)。

編輯:
不要緊,這不適用於多維數組。但是,如果您先將所有數組轉換爲字符串,然後再將它們轉換回數組,則它可以工作。

+1

請注意,array_unique()不適用於多維數組。 – webbiedave

0

通過你的陣列就循環,檢查用戶ID:

$userIds = array(); 

foreach ($users as $key => $user) { 
    if (in_array($user['user_id'], $userIds)) { 
     unset($users[$key]); 
    } else { 
     $userIds[] = $user['user_id']; 
    } 
}