5

I have array in PHP and I have problem with remove 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'd like to remove this repeated elements without 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 doesn't work in this case, because elements are not exactly the same. Can anybody help me how to delete those repeated elements?

3
  • I'd go with sorting and then cycling over the elements. Checking is element n+1 has "mm" in it and deleting item n Commented Mar 11, 2013 at 21:55
  • I can prupose quick solution, maybe it's not the best one, but. array_search('mm)', $array) for all keys, take this values, substr them to needed value 2x 813130B (20mm) -> 2x 813130B; after it array_search this values and unset them. I think it can be improved. Commented Mar 11, 2013 at 21:57
  • 2
    It'd be nice if you provided the array as PHP code to make it easy for us to copy and paste. Commented Mar 11, 2013 at 22:05

3 Answers 3

2

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

    }

  }

It'll iterate over the array, find any elements that have a ( as part of the string, find any elements with a string that matches up to the ( (with extra white space trimmed), and then remove them if it finds it.

3
  • hehe Yoda Conditions Commented Mar 11, 2013 at 22:10
  • Prevents those horrible if ($x = 1) { typos. ;) Commented Mar 11, 2013 at 22:11
  • Sorry for long delay, but I start developing your code immediately. It work perfect. Thanks to you for quicker answer and for everybody. Commented Mar 11, 2013 at 22:17
1
$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;
}
0

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.