PHP How to remove an element from an array - array_uniqe will not work

I have an array in PHP and I have a problem with removing some elements from this array. It looks like this:

Array
(
    [0] => 2x 633130A
    [1] => 2x 525130B
    [2] => 2x 591130B
    [3] => 2x 963130B
    [4] => 2x 813130B (20mm)
    [5] => 2x 813130B
    [6] => 2x 313130B (12mm)
    [7] => 2x 313130B
    [8] => 4x 413130B
    [9] => 2x 633130B
    [12] => 2x 381130A (23mm)
    [13] => 2x 381130A
)

And now I would like to remove these duplicate elements without the mm parameter , as you can see below:

    FROM                =====>              TO          

Array                                   Array
(                                       (
    [0] => 2x 633130A                   [0] => 2x 633130A       
    [1] => 2x 525130B                   [1] => 2x 525130B
    [2] => 2x 591130B                   [2] => 2x 591130B
    [3] => 2x 963130B                   [3] => 2x 963130B
    [4] => 2x 813130B (20mm)            [4] => 2x 813130B (20mm)
    [5] => 2x 813130B <= REMOVE         [5] => 2x 313130B (12mm)
    [6] => 2x 313130B (12mm)            [6] => 4x 413130B
    [7] => 2x 313130B <= REMOVE         [7] => 2x 633130B
    [8] => 4x 413130B                   [8] => 2x 381130A (23mm)
    [9] => 2x 633130B                   )       
    [12] => 2x 381130A (23mm)                   
    [13] => 2x 381130A <= REMOVE
)

I tried array_unique does not work in this case, because the elements are not quite the same. Can someone help me remove these duplicate items?

+5
source share
3 answers

This will do what you need in this case:

  foreach ($array as $value)
  {

    if (FALSE !== strpos($value, '('))
    {

      $string = trim(strtok($value, '('));

      while (FALSE !== $key = array_search($string, $array))
      {
        unset($array[$key]);
      }

    }

  }

, , ( , , ( ( ), , .

+2
$initialData = array(
    '2x 633130A',
    '2x 525130B',
    '2x 591130B',
    '2x 963130B',
    '2x 813130B (20mm)',
    '2x 813130B',
    '2x 313130B (12mm)',
    '2x 313130B',
    '4x 413130B',
    '2x 633130B',
    '2x 381130A (23mm)',
    '2x 381130A',
);

$filteredArray = array_filter(
    $initialData,
    function($value) use ($initialData) {
        if (strpos($value, 'mm') === FALSE) {
            foreach($initialData as $testData) {
                if (strlen($testData) > strlen($value) && 
                    substr($testData,0,strlen($value)) == $value) {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }
);

var_dump($filteredArray);
+1

I wrote a custom function using preg_match()

$array = array
(
    0 => '2x 633130A',
    1 => '2x 525130B',
    2 => '2x 591130B',
    3 => '2x 963130B',
    4 => '2x 813130B (20mm)',
    5 => '2x 813130B',
    6 => '2x 313130B (12mm)',
    7 => '2x 313130B',
    8 => '4x 413130B',
    9 => '2x 633130B',
    12 => '2x 381130A (23mm)',
    13 => '2x 381130A'
);

$results = array();
foreach($array as $element){
    if(!custom_exists($array, $element)){
        $results[] = $element;
    }
}

print_r($results);

function custom_exists($array, $val){
    foreach($array as $element){
        if($element != $val && preg_match("/$val/", $element)){
            return true;
        }
    }
    return false;
}
+1
source

All Articles