Dwza Dwza - 4 months ago 6x
PHP Question

Count specific array values with same part

I found a lot of samples how to count positions or occurrences in an arrays but actualy this doesnt solve my problem.

My Array looks like:

$foo = array(

What I want to achive is to count how many
are in my array.

The thing is, I dont want the count
. So the result for this sample should be 5.

does not help me in this case.

My Try atm is:

$count = 0;
if(array_key_exists("gr".$i, $foo)){

is this the only way to do this ? or is there a nicer way ?

EDIT: Since I need the result for a loop (for) I would like to get rid of this loop.


Alternative solution using array_keys and array_filter functions:

$count = count(array_filter(array_keys($foo), function($v){
    return preg_match("/^gr\d+?/",$v);
// $count is 5