Advertisements
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 values 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. Here's an example:
Example
Try this code »<?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:
- How to remove empty values from an array in PHP
- How to delete an element from an array in PHP
- How to check if a value exists in an array in PHP
Advertisements