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

How to remove identical values ​​from php array

青灯夜游
青灯夜游Original
2021-03-12 17:41:161814browse

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);
?>

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)

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!

Statement:
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