how to search duplicate elements from array and creates unique array

<?php

$allphone = array(
 '11111',
 '22222',
 '33333',
 '44444',
 '55555',
 '55555',
 '66666',
 '+66666',
 '77777',
 '88888',
 '99999',
 '00000');

echo "Mixed Array: "."<br>";
print_r($allphone);
echo "<br>";

$narr = array();
 foreach ($allphone as $value) 
 {
 if( in_array($value, $narr) )
 { 
 }
 else
 {
     array_push($narr, $value);
 } 
}

echo "New Array"."<br>";
print_r($narr);
?>
OUTPUT:
Mixed Array:
Array ( [0] => 11111 [1] => 22222 [2] => 33333 [3] => 44444 [4] => 55555 [5] => 55555 [6] => 66666 [7] => +66666 [8] => 77777 [9] => 88888 [10] => 99999 [11] => 00000 ) 

New Array
Array ( [0] => 11111 [1] => 22222 [2] => 33333 [3] => 44444 [4] => 55555 [5] => 66666 [6] => 77777 [7] => 88888 [8] => 99999 [9] => 00000 )

New array has all unique elements. all duplicate value is removed.

Leave a Reply

Your email address will not be published. Required fields are marked *