Display Unique Number Values in a string using PHP

7:36 PM Unknown 0 Comments

The funciton DisplayUniqueValues() will let you remove the duplicate values from a string array of numbers and sort out the resulting value that will be displayed.

Code:
 
<?php

function DisplayUniqueValues($NumArray)
{
//Remove the duplicated values
$uniques = array_unique(explode(",", $NumArray));

//Sort out the unique values
$result = sort($uniques, SORT_NUMERIC);

//Combine the result in a string using the separator ','
$result = implode(',', $uniques);

return $result;
}

//USAGE
$str_num = '12,13,14,12,12,12,12,11,14,90,78,78,91,90,92,89';

echo DisplayUniqueValues($str_num);

?>
Output should be:
11,12,13,14,78,89,90,91,92

0 comments: