How to remove duplicate values from an array in PHP

Topic: PHP / MySQL« Prev|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:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.