如何计算内部数组计数

时间:2021-09-08 20:22:56

I have an array like as

我有一个类似的数组

$arr[0] = 'summary';
$arr[1]['contact'][] = 'address1';
$arr[1]['contact'][] = 'address2';
$arr[1]['contact'][] = 'country';
$arr[1]['contact'][] = 'city';
$arr[1]['contact'][] = 'pincode';
$arr[1]['contact'][] = 'phone_no';
$arr[2]['work'][] = 'address1';
$arr[2]['work'][] = 'address2';
$arr[2]['work'][] = 'country';
$arr[2]['work'][] = 'city';
$arr[2]['work'][] = 'pincode';
$arr[2]['work'][] = 'phone_no';

Using count($arr) it returns 3 but I also need to count inner array values so it will return 13

使用count($ arr)它返回3但我还需要计算内部数组值,因此它将返回13

What I've tried so far is

到目前为止我尝试过的是

function getCount($arr,$count = 0) {

    foreach ($arr as $value) {

        if (is_array($value)) {
            echo $count;
            getCount($value,$count);
        }
        $count = $count+1;
    }
    return $count;
}

echo getCount($arr);

But its not working as expected

但它没有按预期工作

4 个解决方案

#1


4  

Simply try this

试试吧

function getCount($arr, $count = 0) {
    foreach ($arr as $value) {
        if (is_array($value)) {
            $count = getCount($value, $count);
        } else {
            $count = $count + 1;
        }
    }
    return $count;
}

echo getCount($arr);

What you were doing over here is that you were not storing the value within any variable and that is making an issues with your code as you were on the perfect way

你在这里做的是你没有将值存储在任何变量中,而是因为你在完美的方式上使你的代码出现问题

#2


7  

You can use array_walk_recursive for this. This may help -

您可以使用array_walk_recursive。这可能有所帮助 -

$tot = 0;
array_walk_recursive($arr, function($x) use(&$tot) {
    $tot++;
});

But It is a recursive function so you need to be careful about that.

但它是一个递归函数,所以你需要小心。

In that getCount() method you are not storing the count of the array anywhere. So for every call $count is incremented by 1 only.

在该getCount()方法中,您不会将数组的计数存储在任何位置。因此,对于每次调用,$ count仅增加1。

DEMO

DEMO

#3


4  

If you need to count only the first two levels, you can do a simple foreach, no need to use a recursive function!:

如果你只需要计算前两个级别,你可以做一个简单的foreach,不需要使用递归函数!:

$count = 0;
foreach( $bigArray as $smallArray ){
    if( is_array( $smallArray ) )
          $count += count( $smallArray );
    else
          $count ++;
}

#4


0  

Perhaps I am naive in my approach, but I simply use the sizeof() function. The function's documentation shows that it can take a second argument 1 to tell the function to recursively count multidimensional arrays.

也许我的方法很天真,但我只是使用sizeof()函数。该函数的文档显示它可以采用第二个参数1来告诉函数递归计算多维数组。

So, to get the 'count' you could simply write sizeof($arr, 1); and it should return 13.

因此,要获得'计数',您只需编写sizeof($ arr,1);它应该返回13。

I recognize the value of writing your own function, but doesn't this built-in PHP method solve the problem quite succinctly?

我认识到编写自己的函数的价值,但这种内置的PHP方法是不是非常简洁地解决了这个问题?

#1


4  

Simply try this

试试吧

function getCount($arr, $count = 0) {
    foreach ($arr as $value) {
        if (is_array($value)) {
            $count = getCount($value, $count);
        } else {
            $count = $count + 1;
        }
    }
    return $count;
}

echo getCount($arr);

What you were doing over here is that you were not storing the value within any variable and that is making an issues with your code as you were on the perfect way

你在这里做的是你没有将值存储在任何变量中,而是因为你在完美的方式上使你的代码出现问题

#2


7  

You can use array_walk_recursive for this. This may help -

您可以使用array_walk_recursive。这可能有所帮助 -

$tot = 0;
array_walk_recursive($arr, function($x) use(&$tot) {
    $tot++;
});

But It is a recursive function so you need to be careful about that.

但它是一个递归函数,所以你需要小心。

In that getCount() method you are not storing the count of the array anywhere. So for every call $count is incremented by 1 only.

在该getCount()方法中,您不会将数组的计数存储在任何位置。因此,对于每次调用,$ count仅增加1。

DEMO

DEMO

#3


4  

If you need to count only the first two levels, you can do a simple foreach, no need to use a recursive function!:

如果你只需要计算前两个级别,你可以做一个简单的foreach,不需要使用递归函数!:

$count = 0;
foreach( $bigArray as $smallArray ){
    if( is_array( $smallArray ) )
          $count += count( $smallArray );
    else
          $count ++;
}

#4


0  

Perhaps I am naive in my approach, but I simply use the sizeof() function. The function's documentation shows that it can take a second argument 1 to tell the function to recursively count multidimensional arrays.

也许我的方法很天真,但我只是使用sizeof()函数。该函数的文档显示它可以采用第二个参数1来告诉函数递归计算多维数组。

So, to get the 'count' you could simply write sizeof($arr, 1); and it should return 13.

因此,要获得'计数',您只需编写sizeof($ arr,1);它应该返回13。

I recognize the value of writing your own function, but doesn't this built-in PHP method solve the problem quite succinctly?

我认识到编写自己的函数的价值,但这种内置的PHP方法是不是非常简洁地解决了这个问题?