2013-03-11 86 views
5

我有PHP中的數組,我有問題從這個數組中刪除一些元素。它看起來像這樣:PHP如何從陣列中刪除元素 - array_uniqe將不起作用

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 
) 

現在我想刪除此重複的元素,而毫米參數,你可以看到如下:

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不起作用在這種情況下,因爲元素不完全相同。任何人都可以幫助我如何刪除這些重複的元素?

+0

我會用排序,然後在循環的元素去一個自定義的功能。檢查是元素'n + 1'中有「mm」並刪除項目'n' – 2013-03-11 21:55:07

+0

我可以制定快速解決方案,也許它不是最好的解決方案,但是。 'array_search('mm)',$ array)'對於所有的鍵,取這個值,'substr'它們到需要的值2x 813130B(20mm) - > 2x 813130B;之後它'array_search'這個值並取消設置它們。我認爲可以改進。 – 2013-03-11 21:57:44

+2

如果您將數組作爲PHP代碼提供,以便我們輕鬆複製和粘貼,那將會很不錯。 – MichaelRushton 2013-03-11 22:05:21

回答

2

這會對你在這種情況下需要的東西:

foreach ($array as $value) 
    { 

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

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

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

    } 

    } 

它會遍歷數組過來,發現有(作爲字符串的一部分的任何元素,找到一個匹配字符串的任何元素直到((帶有額外的白色空間修剪),然後如果它發現它們將其刪除。

+0

hehe [Yoda Conditions](http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html) – Chad 2013-03-11 22:10:13

+0

防止那些可怕的'if($ x = 1){'typos。 ;) – MichaelRushton 2013-03-11 22:11:06

+0

對不起,很長的延遲,但我開始立即開發你的代碼。它工作完美。感謝您爲每個人提供更快捷的答案。 – Marcos 2013-03-11 22:17:57

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

我寫了使用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

不錯。我喜歡。 – MichaelRushton 2013-03-11 22:14:28

+0

@MichaelRushton thx :) – HamZa 2013-03-11 22:15:27