Exist some simple function for count only the integer keys of an array?
for example i have this array:
0 => "string"
1 => "string"
"#aaa" => "string"
I need count only the first two element without using a custom foreach loop.
Exist some simple function for count only the integer keys of an array?
for example i have this array:
0 => "string"
1 => "string"
"#aaa" => "string"
I need count only the first two element without using a custom foreach loop.
Do a check on each key to loop through only the numbered keys:
foreach( $arr as $key => $value ) {
if( is_numeric($key) ) { //Only numbered keys will pass
//Do whatever you want
}
}
To count the integer keys, try
count(array_filter(array_keys($array), function($key) {
return is_int($key);
}));
Here's a simple solution:
$int_keys = count(array_filter(array_keys($arr), 'is_int'));