PHP - Remove Duplicate Values From An Array

Submitted by: 
Language: 
Visitors have accessed this post 199 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Remove Duplicate Values From An Array</h3>
  17. <hr style="border-top:1px dotted;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <form>
  21. <div>
  22. <label>Array Content</label>
  23. <div id="result"></div>
  24. </div>
  25. <center><button type="button" class="btn btn-primary" id="remove"><span class="glyphicon glyphicon-trash"></span> Remove Duplicate</button></center>
  26. </form>
  27. </div>
  28. </div>
  29. </body>
  30. <script src="js/jquery-3.2.1.min.js"></script>
  31. <script src="js/script.js"></script>
  32. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.