Home  >  Article  >  Backend Development  >  PHP merge arrays to remove duplicate databases

PHP merge arrays to remove duplicate databases

PHPz
PHPzOriginal
2023-05-19 11:03:07470browse

With the rapid development of the Internet, PHP has become one of the most popular languages ​​in the field of web development. The PHP language has a very good performance when processing arrays. It provides many methods to easily implement array merging, deduplication and other operations. It is also widely used in databases. This article will introduce how to use PHP to merge arrays and remove duplicate content, and apply it to the database.

1. Merging Arrays

First, let’s take a look at how to use PHP to merge two arrays. PHP provides the array_merge() function to complete this operation. The following is a sample code:

$array1 = array('apple', 'banana', 'orange');
$array2 = array('grape', 'peach');
$resultArray = array_merge($array1, $array2);
print_r($resultArray);

Run the above code, the output result is as follows:

Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => peach )

As you can see, $resultArray contains all elements of the two arrays $array1 and $array2.

2. Removing duplicate content

Next, let’s take a look at how to use PHP to remove duplicate content from an array. PHP provides the array_unique() function to perform this operation. The following is a sample code:

$array = array('apple', 'banana', 'orange', 'banana', 'grape', 'orange');
$resultArray = array_unique($array);
print_r($resultArray);

Run the above code, the output result is as follows:

Array ( [0] => apple [1] => banana [2] => orange [4] => grape )

As you can see, only the non-duplicate elements in $array are left in $resultArray.

3. Merge arrays and remove duplication

Combining the above two operations, we can use the following code to complete the array merging and deduplication operation:

$array1 = array('apple', 'banana', 'orange');
$array2 = array('grape', 'peach', 'orange');
$resultArray = array_unique(array_merge($array1, $array2));
print_r($resultArray);

Run the above code and output The results are as follows:

Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => peach )

As you can see, $resultArray contains all non-repeating elements of the two arrays $array1 and $array2.

4. Apply to database

Apply the above operations to the database to easily merge and deduplicate database query results. The following is a sample code:

$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
$sql1 = 'SELECT name FROM table1';
$sql2 = 'SELECT name FROM table2';
$result1 = $pdo->query($sql1)->fetchAll(PDO::FETCH_ASSOC);
$result2 = $pdo->query($sql2)->fetchAll(PDO::FETCH_ASSOC);
$resultArray = array_unique(array_merge(array_column($result1, 'name'), array_column($result2, 'name')));
print_r($resultArray);

The above code extracts a certain column in the database query result (such as the name column in this example), converts it into an array, and then performs merge and deduplication operations. Finally, Output the combined results.

Summary

PHP provides many powerful array operation functions, among which the merging and deduplication functions array_merge() and array_unique() are more commonly used. Applying these operations to the array of database query results can quickly merge and remove duplicates, saving development time and code volume. Hope this article is helpful to you.

The above is the detailed content of PHP merge arrays to remove duplicate databases. 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