【问题标题】:get count of entries in multi-dimensional array with comma separated values- php使用逗号分隔值获取多维数组中的条目数 - php
【发布时间】:2017-02-01 05:28:25
【问题描述】:

如何获取下面数组中的条目数?

我试过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] =>
 );

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:

    使用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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多