1 | initial version |
To determine if there are duplicate values in an array and provide a count for each occurrence, you can use the PHP built-in function "arraycountvalues()". This function takes an array as a parameter and returns an associative array where the keys are the unique values from the original array, and the values are the number of occurrences of each value. Here's an example code:
$array = array("apple", "banana", "apple", "orange", "banana", "grape");
$counted_array = array_count_values($array);
foreach ($counted_array as $key => $value) {
if ($value > 1) {
echo "$key appears $value times\n";
} else {
echo "$key appears $value time\n";
}
}
This code will output:
apple appears 2 times
banana appears 2 times
orange appears 1 time
grape appears 1 time
This means that "apple" and "banana" appear twice each in the original array, while "orange" and "grape" appear only once each.