How to delete the same elements in an array in php

王林
Release: 2023-03-10 21:08:01
Original
2266 people have browsed it

The way PHP deletes the same elements in an array is to pass the array as a parameter to the array_unique function, such as [array_unique(array)]. The array_unique function removes duplicate values ​​from an array and returns the filtered array.

How to delete the same elements in an array in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

There may be duplicate values ​​in an array, so what should we do if we need to delete duplicate values ​​in the array?

We can directly use the array_unique function in php, which can directly remove duplicate values ​​​​in the array and return the filtered array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

Grammar:

array_unique(array)
Copy after login

Code example:

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
Copy after login

Running result:

Array ( [a] => red [b] => green )
Copy after login

Related video tutorial: php video tutorial

The above is the detailed content of How to delete the same elements in an array 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