Saturday, 6 July 2013

Ques: How to find unique array element without any inbuilt function in php?



Answer:
function Find_Unique_Elements($numarray)
{


$count = 0;
$element = $numarray[0];
$result[$count++] = $element;
for ($i = 1; $i < count($numarray); $i++)
{
if ($element == $numarray[$i])
continue;
else
{
$element = $numarray[$i];
$result[$count++] = $element;
}
}
return $result;
}
$array1 = array( 10, 20, 20, 40, 30,25, 30, 35, 40, 45, 50, 55);
$p1 = Find_Unique_Elements($array1);
for($i=0;$i<count($p1);$i++)
{
echo $p1[$i]." , ";
}

No comments:

Post a Comment