Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count array elements which keys are numeric [closed]

Tags:

arrays

php

I have quite a strange array:

Array
(
   [title] => title
   [weight] => 0
   [0] => Text1
   [1] => Text2
   [additional] => Info
}

How do I count array elements which keys are numeric (only)?

like image 560
Randy Avatar asked May 27 '26 12:05

Randy


1 Answers

$data = array(
    'title' => 'title',
    'weight' => 0,
    0 => 'Text1',
    1 => 'Text2',
    'additional' => 'Info'
);

$keyCount = count(
    array_filter(
        array_keys($data),
        'is_numeric'
    )
);

var_dump($keyCount);

EDIT

And from PHP version 5.6.0, you can use

$keyCount = count(
    array_filter($data, 'is_numeric', ARRAY_FILTER_USE_KEY)
);
like image 196
Mark Baker Avatar answered May 30 '26 06:05

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!