2016-08-04 81 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在这种情况下...是无关紧要的)。

在我的示例中,使用了三个哈希值,因为我假定您要分别对待这三个字段。如果你想在任何这三个测试中,对我的例子的改变是微不足道的。