PHP - Remove Duplicate Values From An Array

In this tutorial we will create a Remove Duplicate Values From An Array using PHP. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. The jQuery is a fast, reliable kind of cross-platform javascript library. It is designed to simplify the traditional way of coding in javascript. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Remove Duplicate Values From An Array</h3>
  16.                 <hr style="border-top:1px dotted;"/>
  17.                 <div class="col-md-3"></div>
  18.                 <div class="col-md-6">
  19.                         <form>
  20.                                 <div>
  21.                                         <label>Array Content</label>
  22.                                         <div id="result"></div>
  23.                                 </div>
  24.                                 <center><button type="button" class="btn btn-primary" id="remove"><span class="glyphicon glyphicon-trash"></span> Remove Duplicate</button></center>
  25.                         </form>
  26.                 </div>
  27.         </div>
  28. </body>
  29. <script src="js/jquery-3.2.1.min.js"></script>
  30. <script src="js/script.js"></script>
  31. </html>

Creating PHP Script

This is the main function of the application. This code will display a list of array, then it will remove all duplicate values inside the array To do that copy and write inside the text editor, then save it as shown below. data.php
  1. <?php
  2.         $array = array("Earth", "Jupiter", "Mars", "Neptune", "Saturn", "Venus", "Uranus", "Venus",  "Mercury", "Saturn", "Uranus");
  3.         if(ISSET($_POST['res'])){
  4.                 echo "<pre>";
  5.                 print_r($array);
  6.                 echo "</pre>";
  7.         }
  8.        
  9. ?>
remove.php
  1. <?php
  2.         $array = array("Earth", "Jupiter", "Mars", "Neptune", "Saturn", "Venus", "Uranus", "Venus",  "Mercury", "Saturn", "Uranus");
  3.         if(ISSET($_POST['res'])){
  4.                 $result = array_unique($array);
  5.                 echo "<pre>";
  6.                 print_r($result);
  7.                 echo "</pre>";
  8.         }
  9.  
  10. ?>

Creating jQuery Script

This is where the jQuery function take place on the application.This code will render the php script and display the data to webpages, and send request to php script to remove the duplicate values inside the array. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2.                        
  3.         displayData();
  4.                
  5.         function displayData(){
  6.                 $.ajax({
  7.                         url: "data.php",
  8.                         type: "POST",
  9.                         data: {res: 1},
  10.                         success: function(data){
  11.                                 $('#result').html(data);
  12.                         }
  13.                 });
  14.         }
  15.        
  16.         $('#remove').on('click', function(){   
  17.                 $.ajax({
  18.                         url: "remove.php",
  19.                         type: "POST",
  20.                         data: {res: 1},
  21.                         success: function(data){
  22.                                 $('#result').html("<center><h4>Removing...</h4></center>");
  23.                                 $('#remove').attr('disabled', 'disabled');
  24.                                 setTimeout(function(){
  25.                                         $('#remove').removeAttr('disabled');
  26.                                         $('#result').html(data);
  27.                                 }, 3000);
  28.                         }
  29.                 });
  30.         });
  31. });
There you have it we successfully created a Remove Duplicate Values From An Array using PHP. I hope that these simple tutorial help you understand about PHP. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment