HELOO ,
I have a PHP Variable like :
$var = 'Mark,John,Julian,Mark,Mark,Julian';
i want count every duplicate name and print them like :
John
Mark (3)
Julian (2)
HELOO ,
I have a PHP Variable like :
$var = 'Mark,John,Julian,Mark,Mark,Julian';
i want count every duplicate name and print them like :
John
Mark (3)
Julian (2)
You can do that like this:
$var = 'Mark,John,Julian,Mark,Mark,Julian';
$array = [];
foreach(explode(',', $var) as $name){
if(isset($array[trim($name)]))
$array[trim($name)]++;
else
$array[trim($name)] = 1;
}
foreach($array as $name => $count){
echo $name . ($count > 1 ? ' (' . $count . ')' : '') . '<br />';
}
//prints:
Mark (3)
John
Julian (2)