Removing Duplicates From an array

Removing Duplicates From an array

The following code shows how to remove the duplicates from array.
$arr = array(1,2,3,4,5,5,4,2,11,12,2);
$arr1 = array();

for($i = 0; $i < count($arr); $i++){
$val = $arr[$i];
if(!in_array($val,$arr1)){
array_push($arr1,$val);
}
}

print_r($arr1);

$arr = $arr1;

In the above code, we just stored the unique values to the temporary array called $arr1.

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.