I want to write a function that returns a counter array. That is, given an array of integers $A, in which the integers are in the range (0...$m) it should return an array of size $m + 1 in which each index has the number of occurrences of that index number in $A.
For instance, if:
$A = array(1, 4, 2, 2, 4, 2);
$m = 4;
It should output:
array(0, 1, 3, 0, 2)
I'm wondering if there is a built-in function to do this in PHP.
In python it would look something like:
def counting(A, m):
n = len(A)
count = [0] * (m + 1)
for k in xrange(n):
count[A[k]] += 1
return count
array_count_valuescan help