2016-08-04 82 views
0

我有一個多維數組。 這裏只有兩個記錄的例子,但可以有更多的:PHP - 確保多維數組中的某些值是唯一的

array(2) { 
[0]=> array(7) 
{ 
[0]=> string(0) "" 
[1]=> string(0) "" 
[2]=> string(7) "4646468" 
[3]=> string(1) "1" 
[4]=> string(1) "2" 
[5]=> string(10) "2016-08-18" 
[6]=> string(0) "" 
} 
[1]=> array(7) 
{ 
[0]=> string(0) "" 
[1]=> string(0) "" 
[2]=> string(7) "4646469" 
[3]=> string(1) "1" 
[4]=> string(1) "2" 
[5]=> string(10) "2016-08-18" 
[6]=> string(0) "" 
} 
} 

我需要確保對鍵0,1,和每個內記錄2的值是唯一的。如果其中的任何一個都不是,我想從數組中刪除該記錄(如數組元素的7個值)(但空字符串應該被忽略)。我發現this answer to a similar question成功地輸出了重複對象,但我也想從主數組中刪除它們。問題是我根本不明白這個代碼。我不明白的回調,因此不知道如何修改這個代碼來實現我需要什麼:

$unique = array(); 
foreach($checked as $v) { 
    $key = $v[0] . $v[1] . $v[2]; 
    if (!isset($unique[$key])) 
     $unique[$key] = 0; 
    else 
     $unique[$key]++; 
} 
print_r(array_filter($unique)); 

回答

0

這是我最終定居在:

//make sure either serial, asset tag or hostname have been filled in 
foreach($laptops as $num => $laptop){ 
    if (empty($laptop[0]) && empty ($laptop[1]) && empty($laptop[2])){ 
    $dont_insert[] = $laptop; 
    } else { 
    $do_insert[$num] = $laptop; 
    if($laptop[0]){$hostnames[$num] = $laptop[0];} 
    if($laptop[1]){$asset_tags[$num] = $laptop[1];} 
    if($laptop[2]){$serials[$num] = $laptop[2];} 
    } 
} 
//check user hasn't entered the asset tag, serial or hostname more than once 
if (count(array_unique($hostnames)) < count($hostnames)) { 
    //there are duplicate hostnames 
    $counts = array_count_values($hostnames); 
    $filtered = array_filter($counts, function($value) { 
     return $value != 1; 
    }); 
    $result = array_keys(array_intersect($hostnames, array_keys($filtered))); 
    foreach($result as $dupe){ 
    $dupes[$dupe] = $do_insert[$dupe]; 
    } 
} 
if (count(array_unique($asset_tags)) < count($asset_tags)) { 
    //there are duplicate asset tags 
    $counts = array_count_values($asset_tags); 
    $filtered = array_filter($counts, function($value) { 
     return $value != 1; 
    }); 
    $result = array_keys(array_intersect($asset_tags, array_keys($filtered))); 
    foreach($result as $dupe){ 
    $dupes[$dupe] = $do_insert[$dupe]; 
    } 
} 
if (count(array_unique($serials)) < count($serials)) { 
    //there are duplicate serials 
    $counts = array_count_values($serials); 
    $filtered = array_filter($counts, function($value) { 
     return $value != 1; 
    }); 
    $result = array_keys(array_intersect($serials, array_keys($filtered))); 
    foreach($result as $dupe){ 
    $dupes[$dupe] = $do_insert[$dupe]; 
    } 
} 
//remove any duplicates from our insertion array 
if(count($dupes)){ 
    foreach($dupes as $num => $dupe){ 
    unset($do_insert[$num]); 
    } 
} 
0

你可以做到這一點,修改初始代碼段,你又說:

//$main_array contains your original array data. 

$unique = array(); 

foreach($main_array as $key=>$value) { 
    $key_combination = $value[0] . '-' . $value[1] . '-'. $value[2]; 
    //add the key combination to array if it doesn't initially exists 
    if (!isset($unique[$key_combination])){ 
     $unique[$key_combination] = 0; 
     } 
    //if it gets to this condition means this key combination is a 
    //duplicate and we need to remove the element from main_array 
    else{ 
     unset($main_array[$key]); 
     } 
} 

print_r($main_array);//should contain your original array with duplicates removed 

讓我們來試試這個路線

$unique = array(); 
foreach($main_array as $key=>$value) { 
    $key_combination = $value[0] . '-'. $value[1] . '-'. $value[2]; 
    //add the key combination to array if it doesn't initially exists 
    if (!in_array($key_combination, $unique)){ //This is the change 
     $unique[] = $key_combination;// track duplicates by storing in array 
     } 
    //if it gets to this condition means this key combination is a 
    //duplicate and we need to remove the element from main_array 
    else{ 
     unset($main_array[$key]); 
     } 
} 
+0

感謝這一點 - 我開始以這種方式使用的原代碼,和它的作品除外,出於某種原因,它不工作,如果這三個字段中的一個以上已被填寫。換句話說,如果_only_key [0]中的數據與另一個記錄匹配,它會找到它。但是,如果密鑰1和2也有數據,則代碼不起作用。 – daninthemix

+0

用戶輸入提供的密鑰中是否包含值,並且輸入是否已過濾?試試這個key_combination。 $ key = trim($ v [0])。修剪($ V [1])。修剪([$ v [1]) –

+0

沒有幫助我害怕。它將在三個字段中的任何一個上找到匹配,但只有當這三個匹配字段是唯一的字段時纔可以匹配。 – daninthemix

0

如果你想通過數組進行迭代,並檢查是否有獨特的價值「鍵0,1,2,」處理這些鍵分別在獨特的方面,那麼我可能會定義三個陣列(哈希),一個爲三個,然後做這樣的事情:在每一個關鍵的

$exists0 = array(); $exists1 = array(); $exists2 = array); 
... 
foreach ($array as $record) { 
    if (array_key_exists($exists0, $record[0]) ... you have a duplicate. 
    $exists0[$record[0]] = true; // any value will do ... 

    if (array_key_exists($exists1, $record[1]) ... etcetera. 
} 

存在哈希值爲$exists0, $exists1, $exists2,表示該值是重複的。 (與鍵相關的值... true在這種情況下...是無關緊要的)。

在我的示例中,使用了三個哈希值,因爲我假定您要分別對待這三個字段。如果你想在任何這三個測試中,對我的例子的改變是微不足道的。