Dwza Dwza - 1 year ago 70
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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download