PHP层次数组 - 检查子值是否存在,如果存在则将父属性值赋给其子数组。

3
我们有一个以下的PHP分层数组。
$arr = [
[
    'id' => 1,
    'parent_id' => 0,
    'access_type' => 'full-access',
    'child' => [
        [
            'id' => 4,
            'parent_id' => 1,
            'access_type' => '',
        ],
        [
            'id' => 5,
            'parent_id' => 1,
            'access_type' => '',
        ],
    ],
],
[
    'id' => 2,
    'parent_id' => 0,
    'access_type' => 'read',
    'child' => [
        [
            'id' => 10,
            'parent_id' => 2,
            'access_type' => 'read-write',
            'child' => [
                [
                    'id' => 11,
                    'parent_id' => 10,
                    'access_type' => '',
                ],
                [
                    'id' => 12,
                    'parent_id' => 10,
                    'access_type' => 'read',
                ],
            ],
        ],
        [
            'id' => 7,
            'parent_id' => 3,
            'access_type' => 'read-write',
        ],
    ],
],
[
    'id' => 3,
    'parent_id' => 0,
    'access_type' => 'full-access',
    'child' => [
        [
            'id' => 6,
            'parent_id' => 3,
            'access_type' => '',
            'child' => [
                [
                    'id' => 8,
                    'parent_id' => 6,
                    'access_type' => '',
                ],
                [
                    'id' => 9,
                    'parent_id' => 6,
                    'access_type' => '',
                ],
            ],
        ],
        [
            'id' => 18,
            'parent_id' => 3,
            'access_type' => '',
        ],
    ],
],
[
    'id' => 13,
    'parent_id' => 0,
    'access_type' => '',
    'child' => [
        [
            'id' => 14,
            'parent_id' => 13,
            'access_type' => 'full-access',
            'child' => [
                [
                    'id' => 15,
                    'parent_id' => 14,
                    'access_type' => '',
                ],
                [
                    'id' => 16,
                    'parent_id' => 14,
                    'access_type' => '',
                ],
            ],
        ],
        [
            'id' => 17,
            'parent_id' => 13,
            'access_type' => '',
        ],
    ],
],
];

我们需要像这样的输出。

enter image description here

我们这里有一个层次数组,如果嵌套子元素为空,我想显示access_typefull-access

我们尝试了以下代码。

func_x($arr);

function func_x($arr, $level = 0)
{
    foreach($arr as $x)
    {
        echo str_repeat("---", $level) . " [" . $x['id'] . "] -> " . $x['access_type'] . "<br>";

        if(!empty($x['child']))
        {
            func_x($x['child'], $level+1);
        }
    } 
}

1
在提出代码之前,您能否确认预期输出中[11]的值?在图片中它被设置为'',但如果我理解正确,它应该是“读写”。 - Ivica Pesovski
@IvicaPesovski 对于值[11],我想要空白,我只想更改full-access的值。 - Chirag Pipariya
你为什么想把11为空白,而6、8和9填充为完全访问? - ascsoftw
如果父级拥有“完全访问权限”,那么唯一的子级应该也拥有“完全访问权限”。 - Chirag Pipariya
@ChiragPipariya 请查看我的答案。 - ascsoftw
2个回答

2
您可以使用以下代码:
func_x($arr);

function func_x($arr, $level = 0, $parent = '')
{
    foreach($arr as $x)
    {
        if( empty ( $x['access_type'] ) && $parent == 'full-access' ) {
            $x['access_type'] = $parent;
        }
        echo str_repeat("---", $level) . " [" . $x['id'] . "] -> " . $x['access_type'] . "<br>";

        if(!empty($x['child']))
        {
            func_x($x['child'], $level+1, $x['access_type']);
        }
    } 
}


基本上,您需要将父级访问类型作为第三个参数传递给您的递归函数。
在线演示在这里

@ChiragPipariya 很高兴能帮忙! - ascsoftw

0

你必须将父级访问类型传递给函数

func_x($arr);

function func_x($arr, $level = 0, $parentAccessType = '')
{
    foreach($arr as $x)
    {
        if($x['access_type'] == ''){
            $x['access_type'] = $parentAccessType;
        }
        echo str_repeat("---", $level) . " [" . $x['id'] . "] -> " . $x['access_type'] . "<br>";

        if(!empty($x['child']))
        {
            func_x($x['child'], $level+1, $x['access_type']);
        }
    } 
}

谢谢,但我们需要找到“完全访问权限”的父级,然后只有子级才能拥有“完全访问权限”。 - Chirag Pipariya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接