5

我在 PHP 中有一个数组,我在从这个数组中删除一些元素时遇到了问题。它看起来像这样:

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
)

现在我想删除没有mm参数的重复元素,如下所示:

    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
)

我试过array_unique在这种情况下不起作用,因为元素不完全相同。谁能帮我删除那些重复的元素?

4

3 回答 3

2

在这种情况下,这将满足您的需要:

  foreach ($array as $value)
  {

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

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

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

    }

  }

它将遍历数组,查找字符串中(包含 a 的任何元素,查找字符串匹配的任何元素((修剪多余的空白),然后在找到时将其删除。

于 2013-03-11T22:01:53.937 回答
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);
于 2013-03-11T22:02:06.487 回答
1

我写了一个自定义函数使用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;
}
于 2013-03-11T22:05:00.907 回答