PHP - Remove Duplicate Values From An Array
Submitted by razormist on Friday, August 10, 2018 - 11:14.
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...
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Remove Duplicate Values From An Array</h3>
- <hr style="border-top:1px dotted;"/>
- <div class="col-md-3"></div>
- <div class="col-md-6">
- <form>
- <div>
- <div id="result"></div>
- </div>
- <center><button type="button" class="btn btn-primary" id="remove"><span class="glyphicon glyphicon-trash"></span> Remove Duplicate</button></center>
- </form>
- </div>
- </div>
- </body>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/script.js"></script>
- </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
remove.php
- <?php
- $array = array("Earth", "Jupiter", "Mars", "Neptune", "Saturn", "Venus", "Uranus", "Venus", "Mercury", "Saturn", "Uranus");
- echo "<pre>";
- echo "</pre>";
- }
- ?>
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.- $(document).ready(function(){
- displayData();
- function displayData(){
- $.ajax({
- url: "data.php",
- type: "POST",
- data: {res: 1},
- success: function(data){
- $('#result').html(data);
- }
- });
- }
- $('#remove').on('click', function(){
- $.ajax({
- url: "remove.php",
- type: "POST",
- data: {res: 1},
- success: function(data){
- $('#result').html("<center><h4>Removing...</h4></center>");
- $('#remove').attr('disabled', 'disabled');
- setTimeout(function(){
- $('#remove').removeAttr('disabled');
- $('#result').html(data);
- }, 3000);
- }
- });
- });
- });
Add new comment
- 46 views