daninthemix daninthemix - 4 months ago 14
PHP Question

PHP - ensure certain values within multidimensional array are unique

I have a multidimensional array.
Here's an example with just two records but there could be many more:

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) ""
}
}


I need to make sure that the values for keys 0, 1, and 2 of each inner record are unique. If any of those are not, I want to remove that record (as in, that array element with its 7 values) from the array (however empty strings should be ignored). I found this answer to a similar question which successfully outputs duplicates for me, but I want to also remove them from the main array. The problem is I do not understand that code at all. I don't understand callbacks and therefore don't know how to modify this code to achieve what I need:

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

Answer

This is what I settled on in the end:

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