davidP
davidP

Reputation: 189

How to check if all values in array are identical?

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

Upvotes: 8

Views: 15365

Answers (6)

Dave
Dave

Reputation: 3091

$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";
}

Upvotes: 0

ggfan
ggfan

Reputation: 2492

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

foreach ($array as $newarray){
    echo $newarray. '';
}

Upvotes: -1

kontur
kontur

Reputation: 5217

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

Upvotes: -1

Tom Haigh
Tom Haigh

Reputation: 57845

You could also use this check:

count(array_count_values($arr)) == 1

Upvotes: 3

codaddict
codaddict

Reputation: 455380

You can use the test:

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

Alternatively you can use the test:

$arr === array_fill(0,count($arr),$arr[0]);

Upvotes: 45

brettkelly
brettkelly

Reputation: 28245

$results = array_unique($myArray);
if(count($results) == 1){
   // $myArray is all duplicates
}

Upvotes: 14

Related Questions