PHP 数组,将数组项的计数递归附加到数组

标签 php arrays recursion associative-array

毫无意义的运球


好吧,这是我的另一个奇怪的事情,我要感谢OIS为了帮助我解决我的最后一个问题...它涉及同样类型的时髦数组操作...我深入研究了该代码,我觉得它帮助我更好地使用递归数组操作函数。然而,我再次发现自己陷入了另一个困境

实际问题


我正在尝试编写一个递归操作函数,例如 this 。除了获取每个数组元素的深度之外。 它将为具有一定深度的每个数组元素添加增量计数。了解我正在尝试执行的操作的最简单方法是查看“示例”数组”和“所需结果数组”...我觉得我越来越能更好地理解这些递归函数。但这让我很痛苦,提前感谢您在这方面给我的任何帮助。 请忽略示例数组的 [深度] 结果,我已经有一个添加此结果的函数。 再次感谢, ——你别太刻薄

示例数组


            array (
              52 => 
              array (
                'title' => 'Website Navigation',
                'path' => '',
                'type' => '115',
                'pid' => 0,
                'hasChildren' => 1,
                'children' => 
                array (
                  53 => 
                  array (
                    'title' => 'Home',
                    'path' => '',
                    'type' => '118',
                    'pid' => 52,
                    'hasChildren' => 0,
                  ),
                  54 => 
                  array (
                    'title' => 'Features',
                    'path' => 'features',
                    'type' => '374',
                    'pid' => 52,
                    'hasChildren' => 1,
                    'children' => 
                    array (
                      59 => 
                      array (
                        'title' => 'artistic',
                        'path' => 'features/artistic',
                        'type' => '374',
                        'pid' => 54,
                        'hasChildren' => 1,
                        'children' => 
                        array (
                          63 => 
                          array (
                            'title' => 'galleries',
                            'path' => 'features/artistic/galleries',
                            'type' => '374',
                            'pid' => 59,
                            'hasChildren' => 1,
                            'children' => 
                            array (
                              65 => 
                              array (
                                'title' => 'graphics',
                                'path' => 'features/artistic/galleries/graphics',
                                'type' => '118',
                                'pid' => 63,
                                'hasChildren' => 0,
                              ),
                              67 => 
                              array (
                                'title' => 'mixed medium',
                                'path' => 'features/artistic/galleries/mixed-medium',
                                'type' => '118',
                                'pid' => 63,
                                'hasChildren' => 0,
                              ),
                              64 => 
                              array (
                                'title' => 'overview',
                                'path' => 'features/artistic/galleries',
                                'type' => '118',
                                'pid' => 63,
                                'hasChildren' => 0,
                              ),
                              68 => 
                              array (
                                'title' => 'photography',
                                'path' => 'features/artistic/galleries/photography',
                                'type' => '118',
                                'pid' => 63,
                                'hasChildren' => 0,
                              ),
                              66 => 
                              array (
                                'title' => 'traditional',
                                'path' => 'features/artistic/galleries/traditional',
                                'type' => '118',
                                'pid' => 63,
                                'hasChildren' => 0,
                              ),
                            ),
                          ),
                          62 => 
                          array (
                            'title' => 'overview',
                            'path' => 'features/artistic',
                            'type' => '118',
                            'pid' => 59,
                            'hasChildren' => 0,
                          ),
                          69 => 
                          array (
                            'title' => 'tutorials',
                            'path' => 'features/artistic/tutorials',
                            'type' => '374',
                            'pid' => 59,
                            'hasChildren' => 1,
                            'children' => 
                            array (
                              71 => 
                              array (
                                'title' => 'by category',
                                'path' => 'features/artistic/tutorials/by-category/',
                                'type' => '118',
                                'pid' => 69,
                                'hasChildren' => 0,
                              ),
                              72 => 
                              array (
                                'title' => 'by date',
                                'path' => 'features/artistic/tutorials/by-date/',
                                'type' => '118',
                                'pid' => 69,
                                'hasChildren' => 0,
                              ),
                              70 => 
                              array (
                                'title' => 'overview',
                                'path' => 'features/artistic/tutorials',
                                'type' => '118',
                                'pid' => 69,
                                'hasChildren' => 0,
                              ),
                            ),
                          ),
                        ),
                      ),
                      58 => 
                      array (
                        'title' => 'overview',
                        'path' => 'features',
                        'type' => '118',
                        'pid' => 54,
                        'hasChildren' => 0,
                      ),
                      61 => 
                      array (
                        'title' => 'projects / labs',
                        'path' => 'features/projects-labs/',
                        'type' => '374',
                        'pid' => 54,
                        'hasChildren' => 0,
                      ),
                      60 => 
                      array (
                        'title' => 'web development',
                        'path' => 'features/web-development',
                        'type' => '374',
                        'pid' => 54,
                        'hasChildren' => 1,
                        'children' => 
                        array (
                          74 => 
                          array (
                            'title' => 'articles',
                            'path' => 'features/web-development/articles/',
                            'type' => '374',
                            'pid' => 60,
                            'hasChildren' => 0,
                          ),
                          73 => 
                          array (
                            'title' => 'overview',
                            'path' => 'features/web-development',
                            'type' => '118',
                            'pid' => 60,
                            'hasChildren' => 0,
                          ),
                          75 => 
                          array (
                            'title' => 'tutorials',
                            'path' => 'features/web-development/tutorials',
                            'type' => '374',
                            'pid' => 60,
                            'hasChildren' => 0,
                          ),
                        ),
                      ),
                    ),
                  ),
                  55 => 
                  array (
                    'title' => 'Activity',
                    'path' => 'activity',
                    'type' => '374',
                    'pid' => 52,
                    'hasChildren' => 0,
                  ),
                  56 => 
                  array (
                    'title' => 'Blog',
                    'path' => 'blog',
                    'type' => '374',
                    'pid' => 52,
                    'hasChildren' => 0,
                  ),
                  57 => 
                  array (
                    'title' => 'About',
                    'path' => 'about',
                    'type' => '374',
                    'pid' => 52,
                    'hasChildren' => 1,
                    'children' => 
                    array (
                      76 => 
                      array (
                        'title' => 'the author',
                        'path' => 'about/the-author',
                        'type' => '118',
                        'pid' => 57,
                        'hasChildren' => 0,
                      ),
                      77 => 
                      array (
                        'title' => 'the website',
                        'path' => 'about/the-website',
                        'type' => '118',
                        'pid' => 57,
                        'hasChildren' => 0,
                      ),
                    ),
                  ),
                ),
              ),
            )

所需结果数组
注意[count]索引


            Array
            (
                [53] => Array
                    (
                        [title] => Home
                        [path] => 
                        [type] => 118
                        [pid] => 52
                        [mid] => 53
                        [hasChildren] => 0
                        [depth] => 1
                        [count] =>  1
                    )

                [54] => Array
                    (
                        [title] => Features
                        [path] => features
                        [type] => 374
                        [pid] => 52
                        [mid] => 54
                        [hasChildren] => 1
                        [depth] => 1
                        [count] =>  2
                        [children] => Array
                            (
                                [59] => Array
                                    (
                                        [title] => artistic
                                        [path] => features/artistic
                                        [type] => 374
                                        [pid] => 54
                                        [mid] => 59
                                        [hasChildren] => 1
                                        [depth] => 2
                                        [count] =>  1
                                        [children] => Array
                                            (
                                                [63] => Array
                                                    (
                                                        [title] => galleries
                                                        [path] => features/artistic/galleries
                                                        [type] => 374
                                                        [pid] => 59
                                                        [mid] => 63
                                                        [hasChildren] => 1
                                                        [depth] => 3
                                                        [count] =>  1
                                                        [children] => Array
                                                            (
                                                                [65] => Array
                                                                    (
                                                                        [title] => graphics
                                                                        [path] => features/artistic/galleries/graphics
                                                                        [type] => 118
                                                                        [pid] => 63
                                                                        [mid] => 65
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  1
                                                                    )

                                                                [67] => Array
                                                                    (
                                                                        [title] => mixed medium
                                                                        [path] => features/artistic/galleries/mixed-medium
                                                                        [type] => 118
                                                                        [pid] => 63
                                                                        [mid] => 67
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  2
                                                                    )

                                                                [64] => Array
                                                                    (
                                                                        [title] => overview
                                                                        [path] => features/artistic/galleries
                                                                        [type] => 118
                                                                        [pid] => 63
                                                                        [mid] => 64
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  3
                                                                    )

                                                                [68] => Array
                                                                    (
                                                                        [title] => photography
                                                                        [path] => features/artistic/galleries/photography
                                                                        [type] => 118
                                                                        [pid] => 63
                                                                        [mid] => 68
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  4
                                                                    )

                                                                [66] => Array
                                                                    (
                                                                        [title] => traditional
                                                                        [path] => features/artistic/galleries/traditional
                                                                        [type] => 118
                                                                        [pid] => 63
                                                                        [mid] => 66
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  5
                                                                    )

                                                            )

                                                    )

                                                [62] => Array
                                                    (
                                                        [title] => overview
                                                        [path] => features/artistic
                                                        [type] => 118
                                                        [pid] => 59
                                                        [mid] => 62
                                                        [hasChildren] => 0
                                                        [depth] => 3
                                                        [count] =>  2
                                                    )

                                                [69] => Array
                                                    (
                                                        [title] => tutorials
                                                        [path] => features/artistic/tutorials
                                                        [type] => 374
                                                        [pid] => 59
                                                        [mid] => 69
                                                        [hasChildren] => 1
                                                        [depth] => 3
                                                        [count] =>  3
                                                        [children] => Array
                                                            (
                                                                [71] => Array
                                                                    (
                                                                        [title] => by category
                                                                        [path] => features/artistic/tutorials/by-category/
                                                                        [type] => 118
                                                                        [pid] => 69
                                                                        [mid] => 71
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  1
                                                                    )

                                                                [72] => Array
                                                                    (
                                                                        [title] => by date
                                                                        [path] => features/artistic/tutorials/by-date/
                                                                        [type] => 118
                                                                        [pid] => 69
                                                                        [mid] => 72
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  2
                                                                    )

                                                                [70] => Array
                                                                    (
                                                                        [title] => overview
                                                                        [path] => features/artistic/tutorials
                                                                        [type] => 118
                                                                        [pid] => 69
                                                                        [mid] => 70
                                                                        [hasChildren] => 0
                                                                        [depth] => 4
                                                                        [count] =>  3
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                                [58] => Array
                                    (
                                        [title] => overview
                                        [path] => features
                                        [type] => 118
                                        [pid] => 54
                                        [mid] => 58
                                        [hasChildren] => 0
                                        [depth] => 2
                                        [count] =>  2
                                    )

                                [61] => Array
                                    (
                                        [title] => projects / labs
                                        [path] => features/projects-labs/
                                        [type] => 374
                                        [pid] => 54
                                        [mid] => 61
                                        [hasChildren] => 0
                                        [depth] => 2
                                        [count] =>  3
                                    )

                                [60] => Array
                                    (
                                        [title] => web development
                                        [path] => features/web-development
                                        [type] => 374
                                        [pid] => 54
                                        [mid] => 60
                                        [hasChildren] => 1
                                        [depth] => 2
                                        [count] =>  4
                                        [children] => Array
                                            (
                                                [74] => Array
                                                    (
                                                        [title] => articles
                                                        [path] => features/web-development/articles/
                                                        [type] => 374
                                                        [pid] => 60
                                                        [mid] => 74
                                                        [hasChildren] => 0
                                                        [depth] => 3
                                                        [count] =>  1
                                                    )

                                                [73] => Array
                                                    (
                                                        [title] => overview
                                                        [path] => features/web-development
                                                        [type] => 118
                                                        [pid] => 60
                                                        [mid] => 73
                                                        [hasChildren] => 0
                                                        [depth] => 3
                                                        [count] =>  2
                                                    )

                                                [75] => Array
                                                    (
                                                        [title] => tutorials
                                                        [path] => features/web-development/tutorials
                                                        [type] => 374
                                                        [pid] => 60
                                                        [mid] => 75
                                                        [hasChildren] => 0
                                                        [depth] => 3
                                                        [count] =>  3
                                                    )

最佳答案

我认为这应该可行...我无法在您的示例数组上进行测试,但它似乎适用于我制作的较小数组。

编辑:现在您已从示例数组中删除了“深度”键,因此更改了函数。现在它自己找到深度。我还添加了测试代码和输出:

<?php

function array_depth_count(&$array, $count=array(), $depth=1) {
    foreach ($array as &$value) {
        if (is_array($value)) {
            $value['count'] = ++$count[$depth];
            array_depth_count($value, $count, $depth + 1);
        }
    }
}

$a = array(array(array(array(0),array(0),array(),array()),0,array()));

echo "Before\n";
print_r($a);
array_depth_count($a);
echo "\n\nAfter\n";
print_r($a);

?>

输出:

Before
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 0
                        )

                    [1] => Array
                        (
                            [0] => 0
                        )

                    [2] => Array
                        (
                        )

                    [3] => Array
                        (
                        )

                )

            [1] => 0
            [2] => Array
                (
                )

        )

)

After
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 0
                            [count] => 1
                        )

                    [1] => Array
                        (
                            [0] => 0
                            [count] => 2
                        )

                    [2] => Array
                        (
                            [count] => 3
                        )

                    [3] => Array
                        (
                            [count] => 4
                        )

                    [count] => 1
                )

            [1] => 0
            [2] => Array
                (
                    [count] => 2
                )

            [count] => 1
        )

)

关于PHP 数组,将数组项的计数递归附加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/523413/

相关文章:

php - CSS 菜单 URL 未正确重定向

php - SQL 在 phpMyAdmin 中有效,但在网页脚本中返回错误的行数

c# - 复杂的递归函数 - 在房间和人员之间分配星星

c - 如何用 C 将字符串分成单独的项目(用空格分隔)

javascript - javascript 中是否有与 ruby​​ any 方法等效的方法?

c++ - 相互递归类

javascript - 尝试将嵌套循环转换为递归函数

php - 在终端中解密 PHP 生成的 OpenSSL 字符串

php - 状态设计模式对于 PHP 表单是否过大?

javascript - 这个 Reat Todo list 有什么问题吗?