计数阵列内部的数组? php

发布于 2025-01-23 16:11:02 字数 142 浏览 0 评论 0原文

如何专门计算数组内部的数组? 例如:

$array = [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ]

输出应为3。

How to specifically count arrays inside of an array?
For example:

$array = [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ]

Output should be 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏林苍穹下 2025-01-30 16:11:02

您可以为此使用递归功能。让我给你举个例子:

<?php

$array = [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ];

$arrayCount = 0; # this variable will hold count of all arrays found

# Call a recursive function that starts with taking $array as an input
# Recursive function will call itself from within the function
countItemsInArray($array);


function countItemsInArray($subArray) {

    # access $arrayCount that is declared outside this function
    global $arrayCount;
    
    # loop through the array. Skip if the item is NOT an array
    # if it is an array, increase counter by 1
    # then, call this same function by passing the found array
    # that process will continue no matter how many arrays are nested within arrays
    foreach ($subArray as $x) {
        if ( ! is_array($x) ) continue;

        $arrayCount++;
        countItemsInArray($x);
    }

}


echo "Found $arrayCount arrays\n";

示例

https://rextester.com/sov87180

解释

功能如何运行如何运行。

  • [10,'Hello',[1,2,3,['Hi',7]],[15,67],12]发送到CountiTemsinArray函数
  • 此功能查看阵列
  • 10中的每个项目,都将审查。这不是数组,因此该功能将转到下一个项目
  • “ Hello”。它不是数组,因此该功能转到下一个项目
  • [1、2、3,['hi',7]]。它是一个数组,因此数组增加到1,调用了相同的功能,但是该函数的输入是[1,2,3,['hi',7],7]

请记住,与整个数组一起调用的相同功能还没有死。它只是在等待此countItemsinArray([1,2,3,['hi',7]])要完成

  • 1。它不是数组,因此评估下一个项目
  • 2进行了评估...
  • 3评估...
  • ['HI',7]。这是一个数组。因此,数组计数增加到2
  • countitemsinarray(['hi',7])被调用。

请记住,带有完整数组的CountItemsinArray作为参数 countitemsinarray([[1,2,3,['hi',7]])现在正在等待countitemsinarray(['hi'',', 7])要完成

  • HI。那不是数组,因此函数测试
  • 评估了下一个项目7。那也不是数组。因此,该功能完成了,将控件送回countitemsinarray([1,2,3,['hi',7]]))

countitemsinarray([1,2,3,[[''''嗨,7]]])认识到它无需评估。控制权回到CountItemsinArray($ array)。

  • [15,67]进行了评估。这是一个阵列,因此称为3个
  • CountItemsinArray([15,67]),称为15和16,并确定它们都不是一个

数组($ array)评估12并确定这也不是数组。因此,它结束了其工作。

然后,回声回应了数组计数为3。

You can use a recursive function for that. Let me give you an example:

<?php

$array = [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ];

$arrayCount = 0; # this variable will hold count of all arrays found

# Call a recursive function that starts with taking $array as an input
# Recursive function will call itself from within the function
countItemsInArray($array);


function countItemsInArray($subArray) {

    # access $arrayCount that is declared outside this function
    global $arrayCount;
    
    # loop through the array. Skip if the item is NOT an array
    # if it is an array, increase counter by 1
    # then, call this same function by passing the found array
    # that process will continue no matter how many arrays are nested within arrays
    foreach ($subArray as $x) {
        if ( ! is_array($x) ) continue;

        $arrayCount++;
        countItemsInArray($x);
    }

}


echo "Found $arrayCount arrays\n";

Example

https://rextester.com/SOV87180

Explanation

Here's how the function would operate.

  • [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ] is sent to countItemsInArray function
  • This function looks at each item in the array
  • 10 is reviewed. It's not an array, so the function goes to the next item
  • 'hello' is reviewed. It's not an array, so the function goes to the next item
  • [1, 2, 3, ['hi', 7]] is evaluated. It is an array, so arrayCount is increased to 1 and the same function is called but the input to the function now is [1, 2, 3, ['hi', 7].

Remember, this same function called with the entire array is not dead. It's just waiting for this countItemsInArray([1, 2, 3, ['hi', 7]]) to be done

  • 1 is evaluated. It's not an array, so the next item is evaluated
  • 2 is evaluated ...
  • 3 is evaluated ...
  • ['hi', 7] is evaluated. It is an array. So, array count is increased to 2
  • countItemsInArray(['hi', 7]) is called.

Remember that countItemsInArray with the full array as the parameter AND countItemsInArray([1, 2, 3, ['hi', 7]]) are now waiting for countItemsInArray(['hi', 7]) to be done

  • hi is evaluated. That's not an array, so the function tests the next item
  • 7 is evaluated. That's not an array either. So, the that function completes, giving control back to countItemsInArray([1, 2, 3, ['hi', 7]])

countItemsInArray([1, 2, 3, ['hi', 7]]) recognizes that it has nothing more to evaluate. Control is given back to countItemsInArray($array).

  • [15, 67] is evaluated. It is an array, so array count is increased to 3
  • countItemsInArray([15, 67]) is called, which evaluates 15 and 16 and determines that none of them are an array, so it gives control back to countItemsInArray($array)

countItemsInArray($array) evaluates 12 and determines that's not an array either. So, it ends its work.

Then, echo echoes out that array count is 3.

肤浅与狂妄 2025-01-30 16:11:02

递归迭代您的输入数组,然后在输入数组中的新级别时重新启动TALLY变量。每次遇到一个阵列时,添加一个并重复。从更深层次的层次中传递出级数,当级别完全穿越时,返回顶级的累积计数。

代码:( demo

$array = [10, [[[]],[]], 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12];

function array_tally_recursive(array $array): int {
    $tally = 0;
    foreach ($array as $item) {
        if (is_array($item)) {
            $tally += 1 + array_tally_recursive($item);
        }
    }
    return $tally;
}

echo array_tally_recursive($array);

Recursively iterate your input array and restart the tally variable upon entering a new level in the array. Every time an array is encountered, add one and recurse. Pass up the tallies from the deeper levels and when the levels are completely traversed, return the top level's cumulative tally.

Code: (Demo)

$array = [10, [[[]],[]], 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12];

function array_tally_recursive(array $array): int {
    $tally = 0;
    foreach ($array as $item) {
        if (is_array($item)) {
            $tally += 1 + array_tally_recursive($item);
        }
    }
    return $tally;
}

echo array_tally_recursive($array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文