Home > Backend Development > PHP Problem > How to determine whether the elements in an array are the same in php

How to determine whether the elements in an array are the same in php

青灯夜游
Release: 2023-03-16 07:00:01
Original
4350 people have browsed it

Judgment method: 1. Remove duplicate values ​​in the array, syntax "$newArr=array_unique($arr);"; 2. Get the length of the array after deduplication. If the array length is 1, then the original array The elements in are all the same, the syntax is "if(count($newArr)==1){//Operation when they are all the same}".

How to determine whether the elements in an array are the same in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use array_unique () and count() functions to determine whether the elements in the array are the same.

Implementation idea:

  • Use array_unique() to remove duplicate values ​​in the array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

  • Use count() to obtain the length of the array after deduplication

    • If the array length is 1, it means that there is only one element in the original array , that is, the elements in the original array are all the same

    • If the array length is not 1, it means that not all the elements in the original array are the same

Implementation method:

1. Use array_unique() to remove duplicate values ​​in the array

<?php
$arr1 = array(1,1,1,1,1,1,1);
var_dump(array_unique($arr1));

$arr2 = array(1,2,1,3,1,1,1);
var_dump(array_unique($arr2));
?>
Copy after login

How to determine whether the elements in an array are the same in php

##2. Use count () Determine the length of the array after deduplication and determine whether the array length is 1

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr = array(1,1,1,1,1,1,1);
$newArr=array_unique($arr);
if(count($newArr)==1){
	echo "数组中元素都相同";
}else{
	echo "数组中元素不是都相同";
}
?>
Copy after login

How to determine whether the elements in an array are the same in php

Extension: Improve the code and encapsulate it as a functional function:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
function f($arr){
	$newArr=array_unique($arr);
	if(count($newArr)==1){
		echo "数组中元素都相同<br>";
	}else{
		echo "数组中元素不是都相同<br>";
	}
}
$arr1 = array(1,1,1,1,1,1,1);
$arr2 = array(1,2,1,3,1,1,1);
f($arr1);
f($arr2)
?>
Copy after login

How to determine whether the elements in an array are the same in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to determine whether the elements in an array are the same in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template