I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences.
$input = [1,2,1,3,2,4,10]; //if give string //$input = "hello hello how are you how hello"; //$array = explode(' ',$input); $count_val = []; foreach($array as $val){ $count_val[$val]++; } print_r($count_val); //output ( [1] => 2 [2] => 2 [3] => 1 [4] => 1 [10] => 1 )