How to remove identical data from php array

PHPz
Release: 2023-04-27 17:00:33
Original
612 people have browsed it

In PHP programming, array is a data type we often use. Unlike a single variable, an array can store multiple values. However, sometimes we need to remove duplicate values ​​from an array to ensure data integrity and accuracy. In this article, we will introduce the implementation method of removing identical data in PHP array.

Method 1: Use the array_unique function

The array_unique function in PHP can help us quickly remove duplicate elements from the array and retain only unique values. This function accepts an array as argument and returns a new array containing only unique values.

Sample code:

<?php
  // 创建一个包含重复元素的数组
  $array = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;a&#39;, &#39;d&#39;, &#39;e&#39;, &#39;b&#39;);
  // 使用array_unique函数去除相同的数据
  $unique_array = array_unique($array);
  // 打印去重后的数组
  print_r($unique_array);
?>
Copy after login

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [4] => d
    [5] => e
)
Copy after login
Copy after login

As you can see, the output result only contains unique values ​​and retains their order in the original array.

Method 2: Using loops and in_array function

In addition to using the array_unique function, we can also remove duplicate elements by looping the array and using the in_array function. The in_array function is used to determine whether a value exists in the array. If it exists, it returns true, otherwise it returns false.

Sample code:

<?php
  // 创建一个包含重复元素的数组
  $array = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;a&#39;, &#39;d&#39;, &#39;e&#39;, &#39;b&#39;);
  // 创建一个空数组用于存储唯一的值
  $unique_array = array();
  // 循环数组并去除相同的数据
  foreach($array as $value) {
    if(!in_array($value, $unique_array)) {
      array_push($unique_array, $value);
    }
  }
  // 打印去重后的数组
  print_r($unique_array);
?>
Copy after login

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [4] => d
    [5] => e
)
Copy after login
Copy after login

You can also see that the output result only contains unique values ​​and retains their order in the original array. .

Method 3: Use array_flip function

Another way to remove duplicate data is to use the key of the array to remove duplicates. In PHP, if two or more identical values ​​occur in a given array, the last value overwrites the previous value. Therefore, we can use a handy trick of using the array_flip function to take the values ​​as keys and then convert them back to an array, which will remove duplicate values.

Sample code:

<?php
  // 创建一个包含重复元素的数组
  $array = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;a&#39;, &#39;d&#39;, &#39;e&#39;, &#39;b&#39;);
  // 使用array_flip函数来去除相同的数据
  $unique_array = array_flip($array);
  $unique_array = array_keys($unique_array);
  // 打印去重后的数组
  print_r($unique_array);
?>
Copy after login

Output result:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)
Copy after login

As you can see, the output result only contains unique values ​​and retains their order in the original array.

Summary:

In PHP, you can easily remove duplicate elements from an array using functions such as array_unique, in_array, and array_flip. Which method to choose depends on the specific needs and application scenarios. No matter which method is used, care should be taken to preserve the order in the original array to ensure data integrity and accuracy.

The above is the detailed content of How to remove identical data from php array. For more information, please follow other related articles on the PHP Chinese website!

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