How does PHP tell if an array has the same elements

Remove duplicate values from the array with array_unique(). The syntax “array_unique” will return a de-duplicated array. 2, with count() respectively to obtain the original array and the array length of the re-array, and judge whether the length of the two arrays is equal, the syntax “count(original array)==count(re-array)”, if the two array length is equal, then there is no same element in the array, otherwise there is the same element in the array.

When PHP determines whether an array has the same elements, it determines whether the array elements have duplicate values.

In PHP, you can use the array_unique() and count() functions to determine this.

<!--?</span--> php
header('content-type:text/html; charset=utf-8');
$arr = array (3,4,2,3,6,4,7,2,8,9,1);
var_dump($arr);
$res=array_unique($arr);
Echo "de-weighted array: ";
var_dump($res);
? >

Step 2: Use the count() function to get the length of the original array and the de-duplicated array, and determine whether the two arrays are equal

The count() function counts the number of elements in an array, which is the length of the array.

If two arrays are the same length, there are no duplicate values in the array

If two arrays are not of equal length, it means that an element has been removed, that is, there are duplicate values in the array.

<!--?</span--> php
header('content-type:text/html; charset=utf-8');
$arr = array (3,4,2,3,6,4,7,2,8,9,1);
var_dump($arr);
$res=array_unique($arr);
Echo "de-weighted array: ";
var_dump($res);

$len1=count($arr);
$len2=count($res);
Echo "the length of the original array is: $len1 
";
Echo "The length of the deduplicated array is: $len2 
";

if($len1==$len2){
Echo "two arrays of equal length, are there any identical elements in the array ";
}else{
Echo "two arrays are not the same length, the array has the same elements ";
}
? >

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish