php - 重构具有父/子关系的 n 级数组 (PHP)

标签 php multidimensional-array

我想重组一个数组,stuckoverflow 上的一些解决方案帮助我使其“适合”第一级项目,但您会注意到该数组是 n 级深度。

restructure() 方法没有递归使用(它应该是)。它可能完全错误,并且不知道如何改正。

children 键表示存在具有相应 ID 的子项,parent 键将项目链接到父 ID。

class FilterMenu {

    protected $tree = array();

    static protected $structure = array();

    public function __construct(array $tree)
    {
        $this->tree = $tree;
    }

    public function getStructure()
    {
        self::restructure($this->tree);
        return self::$structure;
    }

    static public function restructure(array $structure)
    {
        foreach ($structure as $k => $v)
        {
            if (isset($v['parent']) and isset($v['children']) and count($v['children']) == 1)
            {
                // only 1 child
                self::$structure[$k] = current(array_keys($v['children']));
            }
            elseif (isset($v['children']))
            {
                $keys = array_keys($v['children']);
                self::$structure[$k] = array_combine($keys, $keys); // mirror array

                //self::restructure($v['children']);
            }
            else
            {
                // no children
                self::$structure[$k] = $k;
            }
        }
    }
}



// test array

$tree = array(
    1 => array(
        'parent' => 1
    ),
    2 => array(
        'parent' => 2,
        'children' => array(
            3 => array(
                'parent' => 2
            ),
            6 => array(
                'parent' => 2,
                'children' => array(
                    10 => array(
                        'parent' => 6,
                        'children' => array(
                            4 => array(
                                'parent' => 10
                            )
                        )
                    )
                ),
            ),
        ),
    ),
    7 => array(
        'parent' => 7,
        'children' => array(
            11 => array(
                'parent' => 7
            )
        )
    ),
    14 => array(
        'parent' => 14,
        'children' => array(
            15 => array(
                'parent' => 14,
            ),
            16 => array(
                'parent' => 14,
            ),
            19 => array(
                'parent' => 14,
            ),
            20 => array(
                'parent' => 14,
            ),
            21 => array(
                'parent' => 14,
            ),
        )
    )
);

// test:
$tree = new FilterMenu($tree);
echo '<pre>'.print_r($tree->getStructure(), true);

实际结果:

Array
(
    [1] => 1
    [2] => Array
        (
            [3] => 3
            [6] => 6
        )

    [7] => 11
    [14] => Array
        (
            [15] => 15
            [16] => 16
            [19] => 19
            [20] => 20
            [21] => 21
        )

)

期望/预期的结果是:

Array
(
    [1] => 1
    [2] => Array
        (
            [3] => 3
            [6] => Array
                (
                    [10] => 4 // <-- array with n-levels...
                )

        )

    [7] => 11
    [14] => Array
        (
            [15] => 15
            [16] => 16
            [19] => 19
            [20] => 20
            [21] => 21
        )

)

这是 link to codepad测试类和数组。

非常感谢任何帮助。谢谢。

最佳答案

我想你正在寻找这个:

function collapse(&$array) {
  foreach ($array as $k => &$v) {
    if (array_key_exists('children', $v)) {
      collapse($v['children']);
      $array[$k] = $v['children'];
    } else {
      $array[$k] = $k;
    }
  }
}

当这样调用时:

collapse($tree);
print_r($tree);

产生:

Array (
  [1]  => 1
  [2]  => Array (
            [3] => 3
            [6] => Array (
                     [10] => Array (
                               [4] => 4
                             )
                   )
          )
  [7]  => Array (
            [11] => 11
          )
  [14] => Array (
            [15] => 15
            [16] => 16
            [19] => 19
            [20] => 20
            [21] => 21
         )
)

参见 http://codepad.org/8atfSWGC

关于php - 重构具有父/子关系的 n 级数组 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645260/

相关文章:

php - 检测字符串是否包含任何数字

c++ - 如何处理非常大的矩阵(例如 1000000 x 1000000)

c - 为什么段错误和程序以退出代码 139 结束?

php - 获取多维数组中的元素计数并指定过滤器

c - 试图创建一个二维列表,但一行的第一个元素将最后一行的最后一个元素更改为所述元素

PHP - 如何读取私有(private) XML 文件?

PhpSpreadsheet - 获取 d/m/Y 日期的原始字符串值,而不是转换为 float

php - MySQL 中数据库的最小值

php - 我应该将访问控制列表序列化为 json 还是数据库表

c++ - 显示二维数组图像的最佳 C++ 库