Home > Backend Development > PHP Problem > How to remove duplicate databases from php array

How to remove duplicate databases from php array

WBOY
Release: 2023-05-06 16:41:07
Original
531 people have browsed it

With the continuous development of Internet technology, databases have become a very important component of a website or application. PHP as a popular programming language is used by many web developers to build dynamic websites. When developing an application, you may need to store data in a database and retrieve data from it. In a database, duplicate data sometimes appears. Duplicate data can take up a lot of storage space and impact web application performance. Therefore, in addition to creating a database, you also need to learn how to remove duplicate data from it. In this article, we will discuss how to remove duplicate database data using PHP arrays.

1. Get data from the database

Before using PHP arrays to remove duplicate database data, we need to get the data from the database first. Here, we use the MySQL database as an example. Suppose we have a table named "student" that contains information such as students' names, ages, genders, and classes. The following is a basic query to get the names of students:

$conn = mysqli_connect("localhost", "username", "password", "dbname");

$sql = "SELECT name FROM student";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

    while($row = mysqli_fetch_assoc($result)) {

        $names[] = $row["name"];

    }

} else {

    echo "0 results";

}

mysqli_close($conn);
Copy after login

In this example, we first connect to the database and then query the names of all students in the "student" table. By using the mysqli_fetch_assoc() function, we can fetch the result set from the database and put it into an array. If the query returns no results, "0 results" is output to the browser.

2. Use PHP arrays to remove duplicate data

Once we have fetched the data from the database and stored it in an array, we can easily use PHP’s built-in function array_unique() Remove duplicate data. The following is a simple example:

$unique_names = array_unique($names);
Copy after login

In this example, we call the array_unique() function and pass in the array $names consisting of student names. This function will return only unique (non-duplicate) names and store them in the variable $unique_names. So if we type $unique_names in the browser, each student's name will be displayed only once.

print_r($unique_names); //输出去重后的学生姓名
Copy after login

3. Insert unique data into the new table

Now we have obtained the unique student names and stored them in the variable $unique_names using PHP’s array_unique() function. We can then insert this unique data into another table. The following is the basic query to insert data into a new table:

$conn = mysqli_connect("localhost", "username", "password", "dbname");

foreach ($unique_names as $name) {

    $sql = "INSERT INTO new_student (name) VALUES ('$name')";

    mysqli_query($conn, $sql);

}

mysqli_close($conn);
Copy after login

In this example, we first connect to the database and use a foreach loop to iterate through the array $unique_names. We then insert each unique name into a new table called "new_student". Finally, we close the connection to the database.

In this way, we can easily remove duplicate data from the database and insert unique data into a new table using PHP arrays.

4. Use the DISTINCT option to obtain unique data

In addition to using PHP arrays, we can also use the DISTINCT option of SQL queries to obtain unique data from the database. Here is the basic query to get a unique list of student names:

$conn = mysqli_connect("localhost", "username", "password", "dbname");

$sql = "SELECT DISTINCT name FROM student";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

    while($row = mysqli_fetch_assoc($result)) {

        echo $row["name"];

    }

} else {

    echo "0 results";

}

mysqli_close($conn);
Copy after login

In this example, we query for student names and get a unique list of names using the DISTINCT option. If the query returns no results, "0 results" is output. Otherwise, we loop through the result set and print each distinct student name.

5. Summary

In this article, we explored how to use PHP arrays to remove duplicate database data. We used a MySQL database as an example and demonstrated how to get student names from a data table and easily remove duplicates using PHP's array_unique() and the DISTINCT option of the SQL query. Finally, we also showed how to insert unique data into a new table. Additionally, deduplication can be achieved using other methods, such as using the GROUP BY option or storing the data in a hash table. But using PHP arrays is a simple and effective method, especially suitable for small applications and databases.

The above is the detailed content of How to remove duplicate databases 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