Question

I have three strings , and I need to compare each string and the have to return unique values, by excluding the repeated values.

$str1 = 1,2,4 ;
$str2 = 2,6 ;
$str3 = 1,4,6 ;

Compare this string and return common value and avoid repeated value.

Output as :

$output = 1,2,4,6

avoid repetition.

Was it helpful?

Solution

Explanation :

Concatenate the three strings and explode them using comma operator in to an array, find the unique entries from that array using array_unique and finally implode it.

echo implode(',',array_unique(explode(',',$str1.','.$str2.','.$str3)));

Demonstration - PHP Native Functions

A regex - solution

$str = preg_match_all('@[0-9]@',$str1.$str2.$str3,$mtch);
echo implode(',',array_unique($mtch[0]));

Demonstration - Regular Expressions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top