Remove Duplicate Value From Array In PHP
Last Updated : Mar 11, 2024
In this article we will show you the solution of remove duplicate value from array in PHP, an array is a collection of homogeneous elements. If an array contains duplicate elements we can removing by using php.
Now php has a built-in function named array_unique() that removes multiple elements with same values.
In this tutorial we will use two different methods :-
- Using for loop
- Using array_unique() method
Step By Step Guide On Remove Duplicate Value From Array In PHP :-
We will see two different methods with examples to remove duplicate values from an array.
Method 1
In the example below we remove duplicate elements manually using a nested For loop.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=, initial-scale=1.0"> <title> remove duplicate value from array in php </title> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> remove duplicate value from array in php </h2> <?php $myArray = array (1,1,2,3,4,5,2,3,4,2) ; for($i=0; $i<count($myArray); $i++) { $j=$i ; $TempArray = $myArray[$i] ; for($k=0; $k<count($myArray);$k++) { if($j!=$k){ if($TempArray==$myArray[$k]) { $myArray[$k] = "" ; } } } } for($x=0;$x<count($myArray);$x++) { echo $myArray[$x] ; } ?> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Created the <style> tag to add CSS to the HTML page.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- we use <?php tag to write PHP within it.
- First create an array with some values.
- Now create a for loop for counting the number of arrays. Then create another for loop within it to remove the duplicate element.
- $TempArray for holding the current values.
- Now create an if statement within an if statement to check $k position.
- Now again use a for loop to print the values.
Method 2
In this method, we are going to use the array_unique() method.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=, initial-scale=1.0"> <title> remove duplicate value from array in php </title> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> remove duplicate value from array in php </h2> <?php $myArray = array (1,1,2,3,4,5,2,3,4,2) ; $a = array_unique($myArray) ; print_r($a) ?> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Created the <style> tag to add CSS to the HTML page.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- we use <?php tag to write PHP within it.
- First create an array with some values.
- Using the array_using() function that removes the duplicate elements.
- Using print_r() to display the values.
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know to remove duplicate value from array using PHP.
I hope this article on remove duplicate value from array in PHP helps you and the steps and method mentioned above are easy to follow and implement.