How to count array elements after the first occurrence of a given element?
For example
$a = array('a', 'c','xxd','ok','next1','next2');
echo occurences_after($a, 'ok');
should print "2"
How to count array elements after the first occurrence of a given element?
For example
$a = array('a', 'c','xxd','ok','next1','next2');
echo occurences_after($a, 'ok');
should print "2"
If you're looking for the number of elements after the first occurrence of a given element:
function occurences_after($array, $element)
{
if ( ($pos = array_search($element, $array)) === false)
return false;
else
return count($array) - $pos - 1;
}
If I read your question correctly, the way to count the number of elements that match a particular element in an array is to loop over the array and check each element individually for a match. This smells like homework, so I'm not going to write the code for you, because it's trivial, but it should get you going in the right direction.
So to reiterate:
I'm not sure what you mean. If you want to count the amount of elements in the array $A.
You can use count($A)
If you want to count the values:
print_r(array_count_values($A));
The output will be:
Array ( [a] => 1 [c] => 1 [xxd] => 1 [ok] => 1 [next1] => 1 [next2] => 1 )