All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Remove Duplicate Value From Array In PHP

Last Updated : Mar 11, 2024

Remove Duplicate Value From Array In PHP

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 :-

  1. Using for loop
  2. 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>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Created the <style> tag to add CSS to the HTML page.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. we use <?php tag to write PHP within it.
  8. First create an array with some values.
  9. Now create a for loop for counting the number of arrays. Then create another for loop within it to remove the duplicate element.
  10. $TempArray for holding the current values.
  11. Now create an if statement within an if statement to check $k position.
  12. 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>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Created the <style> tag to add CSS to the HTML page.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. we use <?php tag to write PHP within it.
  8. First create an array with some values.
  9. Using the array_using() function that removes the duplicate elements.
  10. 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.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪