说我有这样的数组:
array('a string', 23, array(array('key'=>'value'), 67, 'another string'), 'something else')
Run Code Online (Sandbox Code Playgroud)
我想知道我的数组有多少个值,排除了作为主数组成员的数组.怎么样?(预期结果是6)
由于问题本身,Foreach循环不适合 - 数组的未知深度.
有谁知道如何实现这个?
你可以使用array_walk_recursive.
$count = 0;
array_walk_recursive($arr, function($var) use (&$count) {
$count++;
});
echo $count;
Run Code Online (Sandbox Code Playgroud)