How to check if all values in array are identical?

后端 未结 6 1965
北恋
北恋 2021-01-05 09:42

In PHP how can I quickly tell if all values in array are identical?

相关标签:
6条回答
  • 2021-01-05 10:17

    You can check for count(array_intersect($arr1, $arr2)) == 0

    0 讨论(0)
  • 2021-01-05 10:24
    $results = array_unique($myArray);
    if(count($results) == 1){
       // $myArray is all duplicates
    }
    
    0 讨论(0)
  • 2021-01-05 10:24

    Do a test run and see if all results are the same:

    foreach ($array as $newarray){
        echo $newarray. '';
    }
    
    0 讨论(0)
  • 2021-01-05 10:29

    You could also use this check:

    count(array_count_values($arr)) == 1
    
    0 讨论(0)
  • 2021-01-05 10:34

    You can use the test:

    count(array_unique($arr)) == 1;
    

    Alternatively you can use the test:

    $arr === array_fill(0,count($arr),$arr[0]);
    
    0 讨论(0)
  • 2021-01-05 10:40
    $myArray = array('1','1','1');
    $results = array_unique($myArray);
    if(count($results) == 1)
    {
        echo"all value is duplicates";
    }
    else
    {
        echo"all value is not duplicates";
    }
    
    0 讨论(0)
提交回复
热议问题