2015-01-01 34 views
1

我知道如何使用array_diff_key($x , array_unique($x));來檢測PHP數組中的重複值。檢測重複但忽略PHP數組上的值

我的問題是我想檢測PHP數組中的重複值,但忽略一個值或NULL值。例如:

$x = array (1,0,0,0,4,4,3); 
$x = array (1,NULL,NULL,NULL,4,4,3); 

我想檢測4而不更改數組結構(數組長度仍然必須爲7)。 這是可能的PHP?怎麼做?

+0

'這是可能在PHP'是的,這是可能的PHP –

+0

如何做到這一點? – john

+1

1.閱讀本網站的規則2.編寫代碼3.發佈清除問題 –

回答

1

試試這個:

$y只有項目NOT NULL。

$z具有來自$x的非空的重複項的鍵。

0

我可以做以下的自定義函數detectArr

<?php 
$x = array (1,0,0,0,4,4,3); 

    function detectArr($arr){ 
     $output = array(); 
     foreach ($arr as $i){ 
      if ($i){ 
        $output[] = $i; 
      } 
      else{ 
        $output[] = NULL; 
      }   
     } 
     return $output; 
    } 

    echo "<pre>"; 
    print_r(detectArr($x)); 

結帳這個DEMO:http://codepad.org/W8ocL6dj