2017-07-19 70 views
1

多個相同的價值觀我有一個像下面檢查數組包含在PHP

$array1 = Array 
(
    [0] => 20225,2017-02-20 
    [1] => 20225,2017-02-20 
    [2] => 10027,2017-02-20 
    [3] => 10027,2017-02-20 
    [4] => 10021,2017-02-20 
    [5] => 20205,2017-02-20 
    [6] => 50003,2017-02-20 
    [7] => 20225,2017-02-20 
    [8] => 20205,2017-02-20 
    [9] => 10021,2017-02-20 
    [10] => 50003,2017-02-20 
    [11] => 10027,2017-02-20 
    [12] => 20225,2017-02-20 
    [13] => 20225,2017-02-21 
    [14] => 20225,2017-02-21 
) 

一個數組,如果數組包含相同的結果大於2,我想這些值到另一個陣列的一個叫$array2

請幫我像你說的這樣做

+0

你嘗試過這麼遠嗎?請閱讀:[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)以及[我如何問一個好問題?](http://stackoverflow.com/幫助/如何問) –

+0

你的問題標題和你想要的是完全兩個完全不同的東西 –

回答

1

: - 如果陣列包含同樣的結果大於2,我想這些值到另一個陣列的一個叫$array2.

你可以像下面: -

$array_new = array_count_values($array1); 
$array2 = array(); 
foreach($array_new as $key=>$val){ 
    if($val >1){ //or do $val >2 based on your desire 
     $array2[] = $key; 
    } 
} 
print_r($array2); 

輸出: - https://eval.in/834306https://eval.in/834402

如果你想檢查數組包含重複或沒有,那麼你可以這樣做: -

if(count(array_unique($array1)) < count($array1)){ 
    echo "Array have some duplicates"; 
}else{ 
    echo "Array have unique elements"; 
} 

輸出: - https://eval.in/834312

如果你想只得到獨特的數組,那麼你可以這樣做下面: -

$array1 = array_values (array_unique($array1)); 

輸出: - https://eval.in/834317

+1

**如果數組包含超過2 **的相同結果,所以我將'$ val> 1'更改爲'$ val> 2'。很好的回答:) !!! – tenten

+0

@tenten很高興幫助你:) –

1
<?php 
$array1 = [ 
    "20225,2017-02-20", 
    "20225,2017-02-20", 
    "10027,2017-02-20", 
    "10027,2017-02-20", 
    "10027,2017-02-20", 
    "10021,2017-02-20" 
]; 

$array_unique = []; 
$array2 = []; 

foreach($array1 as $value) { 
    if(!in_array($value, $array_unique)) { 
     $array_unique[] = $value; 
    } else { 
     $array2[] = $value; 
    } 
} 

echo 'Unique Values: '; 
print_r($array_unique); 

echo '<hr /> Duplicate Values: '; 
print_r(array_unique($array2)); 

入住: - http://phptester.net/

2

你可以使用這個簡單的函數來檢查,如果你的陣列有重複:

function array_has_dupes($array) { 
    return count($array) !== count(array_unique($array)); 
} 

這裏的用法:

<?php 

$array = Array 
(
    '0' => '20225,2017-02-20', 
    '1' => '20225,2017-02-20', 
    '2' => '10027,2017-02-20', 
    '3' => '10027,2017-02-20', 
    '4' => '10021,2017-02-20', 
    '5' => '20205,2017-02-20', 
    '6' => '50003,2017-02-20', 
    '7' => '20225,2017-02-20', 
    '8' => '20205,2017-02-20', 
    '9' => '10021,2017-02-20', 
    '10' => '50003,2017-02-20', 
    '11' => '10027,2017-02-20', 
    '12' => '20225,2017-02-20', 
    '13' => '20225,2017-02-21', 
    '14' => '20225,2017-02-21' 
); 

$arr_unique = array_unique($array); 
$check = count($array) !== count($arr_unique); 
$message = 'No duplicates found.'; 
$arr_duplicates = []; 
if($check == 1) { 
    $message = "Duplicates found."; 
    $arr_duplicates = array_diff_assoc($array, $arr_unique); 
} 

echo $message; 
print_r($arr_duplicates); 
?> 

順便說一句,array_unique()是什麼你正在尋找。

1
$array1 = [ 
    [20225,'2017-02-20'], 
    [20225,'2017-02-20'], 
    [10027,'2017-02-20'], 
    [10027,'2017-02-20'], 
    [10021,'2017-02-20'], 
    [20205,'2017-02-20'], 
    [50003,'2017-02-20'], 
    [20225,'2017-02-20'], 
    [20205,'2017-02-20'], 
    [10021,'2017-02-20'], 
    [50003,'2017-02-20'], 
    [10027,'2017-02-20'], 
    [20225,'2017-02-20'], 
    [20225,'2017-02-21'], 
    [20225,'2017-02-21'] 
]; 

$array2 = array_map("unserialize", array_unique(array_map("serialize", $array1))); 

echo '<pre>'; 
print_r(($array2));` 

它的結果會是怎樣

Array 
(
    [0] => Array 
     (
      [0] => 20225 
      [1] => 2017-02-20 
     ) 

    [2] => Array 
     (
      [0] => 10027 
      [1] => 2017-02-20 
     ) 

    [4] => Array 
     (
      [0] => 10021 
      [1] => 2017-02-20 
     ) 

    [5] => Array 
     (
      [0] => 20205 
      [1] => 2017-02-20 
     ) 

    [6] => Array 
     (
      [0] => 50003 
      [1] => 2017-02-20 
     ) 

    [13] => Array 
     (
      [0] => 20225 
      [1] => 2017-02-21 
     ) 

) 
+0

你仔細閱讀過問題嗎? –