I have an array where I fill in the types as string of each element. For example:
Types array
type1 | type2 | type2 | type3 | type2 | type1 | type3
$types = array('type1', 'type2', 'type2', 'type3', 'type2', 'type1', 'type3')
Now I want to count the occurrence of each type as I iterate the array.
For example:
when I am at the first element of the array I want to return:
type1 : 1
type2 : 0
type3 : 0
When I am at the fourth element I want:
type1 : 1
type2 : 2
type3 : 1
Actually, I am only interested to find the occurrence of the type of the element which I am looking. For example: fourth element
type3: 1
Is there a php function to do that? Or I will have to iterate the whole array and count the occurrences of types ?
Thanks
There is not a native function to do this. But we can write a simple one:
This code produces this: