簡體   English   中英

PHP-確保多維數組中的某些值是唯一的

[英]PHP - ensure certain values within multidimensional array are unique

我有一個多維數組。 這是一個只有兩個記錄的示例,但可能還有更多記錄:

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個值)(但是應該忽略空字符串)。 我找到了類似問題的答案,該問題為我成功輸出了重復項,但我也想從主陣列中將其刪除。 問題是我根本不理解該代碼。 我不了解回調,因此不知道如何修改此代碼來實現我所需要的:

$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));

您可以執行以下操作,修改您添加的初始代碼段:

//$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、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 ...是無關緊要的。)

在我的示例中,使用了三個散列,因為我假設您想分別對待這三個字段。 如果要測試這三個中的任何一個,都可以,對我的示例所做的更改是顯而易見的。

這是我最終確定的:

//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]);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM