如何获取下面数组中的条目数?
我试过Count arrays comma separated values并没有得到想要的解决方案。请建议一种方法。
以下样本数为 11。
$sample= Array (
[0] => Array (
[0] => Array ( [attendance] => 2012SD71,2010SD94 )
[1] => Array ( [attendance] => 2003SD18,2003SD19 )
[2] => Array ( [attendance] => 2003SD23,2003SD28 ))
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => Array (
[0] => Array ( [attendance] => 2012SD81,2010SD84 )
[1] => Array ( [attendance] => 2003SD18,2003SD19,2004SD14 ) ) [14] =>
[15] =>
);
最佳答案
一个简单的递归函数应该可以使用 explode()
和 array_merge()
:
function recurse($array,&$new)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurse($value,$new);
}
else {
if(!empty($value)) {
$exp = array_filter(explode(',',$value));
$new = array_merge($new,$exp);
}
}
}
}
# Create a storage array
$new = array();
# Run the recursive function
recurse($sample,$new);
# Show count
print_r(count($new));
关于php - 获取具有逗号分隔值的多维数组中的条目计数 - php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972507/