Thursday, January 15, 2009

Remove duplicate values from an array in php - array_unique

You can use array_unique() function get unique values out of a long array.
function will return an array with unique values and will preserve the keys.


<?php

$my_array = array(1, 2, 3, 3, 4, 4, 1, 5, 6, 6, 7);

$my_unique_array = array_unique($my_array);

echo '<pre>';
print_r($my_unique_array);
echo '</pre>';

/*
OUTPUT:

Array
(
[0] => 1
[1] => 2
[2] => 3
[4] => 4
[7] => 5
[8] => 6
[10] => 7
)


*/
?>

No comments:

Post a Comment