繁体   English   中英

计算数组元素哪些键是数字[关闭]

[英]Count array elements which keys are numeric [closed]

我有一个很奇怪的数组:

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

如何计算哪些键是数字(仅)的数组元素?

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

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

var_dump($keyCount);

编辑

从PHP 5.6.0版开始,您可以使用

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

应该安静简单:

$i = 0;
foreach  ($arr as $k => $v) {
    if (is_numeric($k))
        $i++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM