0

I want to remove some duplicate values on an array, but there is a condition that the script has to ignore the array that contains a specific word.

Below code is adapted from PHP: in_array.

$array = array( 'STK0000100001',
                'STK0000100002',
                'STK0000100001',           //--> This should be remove
                'STK0000100001-XXXX',      //--> This should be ignored
                'STK0000100001-XXXX' );    //--> This should be ignored

$ignore_values = array('-XXXX');

if(make_unique($array, $ignore_values) > 0) {
    //ERROR HERE
}

The function to make the array unique is:

function make_unique($array, $ignore) {
    $i = 0;
    while($values = each($array))  {
        if(!in_array($values[1], $ignore)) {
            $dupes = array_keys($array, $values[1]);
            unset($dupes[0]);
            foreach($dupes as $rmv) {
                $i++;
            }            
        }
    }
    return $i;
}

I have tried to use if(!in_array(str_split($values[1]), $ignore)) ... but it just the same.

The array should become like:

STK0000100001
STK0000100002
STK0000100001-XXXX
STK0000100001-XXXX

How to do that?

2 Answers 2

1

Try this one, just remove the print_r(); inside the function when using in production

if(make_unique($array, $ignore_values) > 0) {
    //ERROR HERE
}

function make_unique($array, $ignore)  {
    $array_hold = $array;
    $ignore_val = array();
    $i = 0;
    foreach($array as $arr) {
        foreach($ignore as $ign) {
            if(strpos($arr, $ign)) { 
                 array_push( $ignore_val, $arr);
                 unset($array_hold[$i]);
                 break;
            }
        }
        $i++;
    }
    $unique_one = (array_unique($array_hold));
    $unique_one = array_merge($unique_one,$ignore_val);
    print_r($unique_one);

    return count($array) - count($unique_one);
}
Sign up to request clarification or add additional context in comments.

Comments

1

This should work for >= PHP 5.3.

$res = array_reduce($array, function ($res, $val) use ($ignore_values) { 
    $can_ignore = false;
    foreach ($ignore_values as $ignore_val) {
      if (substr($val, 0 - strlen($ignore_val)) == $ignore_val) {
        $can_ignore = true;
        break;
      }
    }

    if ( $can_ignore || ! in_array($val, $res)) {
      $res[] = $val;
    }
    return $res;

  }, array()
);

Otherwise

$num_of_duplicates = 0;
$res = array();
foreach ($array as $val) { 
  $can_ignore = false;
  foreach ($ignore_values as $ignore_val) {
    if (substr($val, 0 - strlen($ignore_val)) == $ignore_val) {
      $num_of_duplicates++;
      $can_ignore = true;
      break;
    }
  }
  if ( $can_ignore || ! in_array($val, $res)) {
    $res[] = $val;
  }
}

Edit: Added duplicate count to the second snippet.

1 Comment

Hi, thanks for your code. It solved my problem but it's not returning any counting value to identify if there was some value removed from the array. And btw I already have the answer. Thanks again! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.