Removing Duplicates From an array

Removing Duplicates From an array

Following example will show how to remove duplicate from an array

$arr = array("one"=>1,"two"=>2,3,4,5,6,5,7,"one1"=>1,2);

$tempArr = array_flip ($arr);
$tempArr1 = array_flip ($tempArr);

print_r($tempArr1);

Explanation
array_flip() function will get an array as argument and return an array in flip order that means key and value are interchanged.If value is found n times , latest key will be return as value.

So in the above code

$tempArr = array_flip ($arr);

$tempArr will contain

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

then again we do the flip
$tempArr1 = array_flip ($tempArr);

Now the duplicates are removed

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.