There is such a function in php as count, with its help you can find out the number of elements in the array. Is it possible to count the number of array values, the keys of which are only numbers?

There is for example such an array:

Array ( 0 => 'val 0', 1 => 'val 1', 2 => 'val 2', 'key 3' => 'val 3' ); 

If you use the count function, then the value will return 4. Is there a function in php that would give the answer in this case 3?

    1 answer 1

    Filter by numeric keys ( in the example, using the is_int function ) and count the number of elements:

     <?php echo count(array_filter($arr, 'is_int', ARRAY_FILTER_USE_KEY)); 

    Just in case, if you have PHP version <5.6

     <?php echo count(array_filter(array_keys($arr), 'is_int')); 
    • Gorgeous, thank you very much! - johndws 4:05 pm