Home > Backend Development > PHP Problem > How to remove identical values ​​from php array

How to remove identical values ​​from php array

青灯夜游
Release: 2023-03-08 17:32:01
Original
1871 people have browsed it

In php, you can use the array_unique() function to remove the same values ​​​​in the array, the syntax format is "array_unique(array)". If there are two or more values ​​in the array that are the same, use the array_unique() function to keep only the first value and the other values ​​will be removed.

How to remove identical values ​​from php array

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Remove the same values ​​from the php array

In PHP, you can use the array_unique() function to remove identical values ​​from an array. Example:

<?php
$arr1 =array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
var_dump($arr1);
$arr2=array_unique($arr1);
var_dump($arr2);
?>
Copy after login

Output:

How to remove identical values ​​from php array

Related function introduction:

array_unique() function shift Removes duplicate values ​​in an array and returns the resulting array. When the values ​​of several array elements are equal, only the first element is retained and the other elements are deleted. The key names in the returned array remain unchanged.

Grammar:

array_unique(array)
Copy after login

How to remove identical values ​​from php array

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of How to remove identical values ​​from php array. 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