WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to remove duplicate values from an array in PHP

Topic: PHP / MySQLPrev|Next

Answer: Use the PHP array_unique() function

You can use the PHP array_unique() function to remove the duplicate elements or vlaues form an array. If the array contains the string keys, then this function will keep the first key encountered for every value, and ignore all the subsequent keys.

  • <?php
  • $array = array("a" => "moon", "star", "b" => "moon", "star", "sky");
  •  
  • // Deleting the duplicate items
  • $result = array_unique($array);
  • print_r($result);
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

Your Feedback:

We would love to hear from you, please drop us a line.