Check if array has same values php

Star

Embed

What would you like to do?

PHP : Check if array has duplicate values

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

โฎ PHP Array Reference

Example

Remove duplicate values from an array:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>

Try it Yourself ยป


Definition and Usage

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.

Note: The returned array will keep the first array item's key type.


Syntax

array_unique(array, sorttype)

Parameter Values

ParameterDescription
array Required. Specifying an array
sorttype Optional. Specifies how to compare the array elements/items. Possible values:
  • SORT_STRING - Default. Compare items as strings
  • SORT_REGULAR - Compare items normally (don't change types)
  • SORT_NUMERIC - Compare items numerically
  • SORT_LOCALE_STRING - Compare items as strings, based on current locale


Technical Details

Return Value:Returns the filtered array
PHP Version:4.0.1+
PHP Changelog:PHP 7.2: If sorttype is SORT_STRING, this returns a new array and adds the unique elements.
PHP 5.2.9: The default value of sorttype was changed to SORT_REGULAR.
PHP 5.2.1: The default value of sorttype was changed back to SORT_STRING.

โฎ PHP Array Reference


Check if all Values in an Array are Equal #

To check if all values in an array are equal:

  1. Use the Array.every() method to iterate over the array.
  2. Check if each array element is equal to the first one.
  3. The every method only returns true if the condition is met for all array elements.

Copied!

const arr1 = [1, 1, 1]; const arr2 = [1, 1, 2]; function allAreEqual(array) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ false

The function we passed to the Array.every method gets called with each element in the array until it returns a falsy value or iterates over the entire array.

If the function returns a falsy value, the every method short-circuits and returns false.

On each iteration, we check if the current element is equal to the element at position 0. If the condition is met for all elements, then they are all equal.

When the every method is called on an empty array it returns true for any condition.

Copied!

const arr1 = []; function allAreEqual(array) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ true

Whether you need to handle this depends on your use case. If consider an empty array as an array where all values are equal, you don't need to do anything.

However, if you consider an empty array one where not all elements are equal, use the following solution.

Copied!

const arr1 = []; const arr2 = [1, 1, 1]; function allAreEqual(array) { if (array.length > 0) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } return false; } console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ false console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ true

We first check for the array's length and we only call the every method if the array contains at least 1 element.

This is entirely use case specific. For example, you might want to check if the array has at least 2 elements to return true from the function.

An alternative approach is to use a Set object.

Check if all Values in an Array are Equal using Set #

To check if all values in an array are equal:

  1. Pass the array to the Set constructor and access the size property.
  2. The Set object only stores unique values.
  3. If the Set has a length of 1, then all array elements are equal or the array only contains 1 element.

Copied!

const arr1 = [1, 1, 1]; const arr2 = [1, 1, 2]; function allAreEqual(array) { const result = new Set(array).size === 1; return result; } console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ false

The Set object allows us to store unique values and removes all duplicates automatically.

If we pass it an array containing the same value multiple times, it would only get added once to the Set.

Copied!

console.log(new Set([1, 1, 1])); // ๐Ÿ‘‰๏ธ { 1 }

The size property of the Set allows us to get the number of values stored in the Set.

If the number of values in the Set is equal to 1, then all of the values in the array are equal or the array only contains one element.

If you only want to return true if the array contains at least 2 elements, add the following if statement.

Copied!

const arr1 = [1]; const arr2 = [1, 1, 1]; function allAreEqual(array) { if (array.length > 1) { const result = new Set(array).size === 1; return result; } else { return false; } } console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ false console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ true

We first make sure the array contains more than 1 element. If it does, we pass the array to the Set constructor.

In all other cases, we return false.

How can I check if two arrays have same value in PHP?

Now, to check whether two arrays are equal or not, an iteration can be done over the arrays and check whether for each index the value associated with the index in both the arrays is the same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements is not important.

How do you check if an array has the same value?

To check if all values in an array are equal: Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one.

How do you check if all the elements in an array are same PHP?

php - Check if all values in array are the same - Stack Overflow.
if (array_unique($allvalues) === array('foobar')) {.
// all values in array are "foobar".

How do you check if an array has a value in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.