如何从分层树导出产品完整类别路径?

时间:2014-07-03 07:26:53

标签: php recursion hierarchical-data virtuemart

我的出发点是一个包含类别ID及其父ID的数组,来自Virtuemart类别表,如下所示:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [name] => Category A
        )

    [1] => Array
        (
            [id] => 2
            [parent_id] => 1
            [name] => Subcategory A1
        )

    [2] => Array
        (
            [id] => 3
            [parent_id] => 2
            [name] => Sub-Subcategory A1
        )

    [3] => Array
        (
            [id] => 4
            [parent_id] => 1
            [name] => Subcategory A2
        )

    [4] => Array
        (
            [id] => 5
            [parent_id] => 0
            [name] => Category B
        )

    [5] => Array
        (
            [id] => 6
            [parent_id] => 5
            [name] => Subcategory B
        )

)

我需要有一个字符串,其中每个最后一个类别的子完整路径在子节点之间用/(斜杠)和第一级父节点之间的(逗号)分隔

我的输出应该是:

Category A/Subcategory A1/Sub-Subcategory A1,Category A/Subcategory A2,Category B/Subcategory B

[编辑] 我可以将起始数组转换为这个

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [name] => Category A
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [name] => Subcategory A1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [parent_id] => 2
                                            [name] => Sub-Subcategory A1
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 4
                            [parent_id] => 1
                            [name] => Subcategory A2

                        )

                )

        )

    [1] => Array
        (
            [id] => 5
            [parent_id] => 0
            [name] => Category B
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [parent_id] => 5
                            [name] => Subcategory B1

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:1)

给出下一个数组:

$array = Array(
    "0" => Array
        (
        "id" => 1,
        "parent_id" => 0,
        "name" => "Category A"
    ),
    "1" => Array
        (
        "id" => 2,
        "parent_id" => 1,
        "name" => "Subcategory A1"
    ),
    "2" => Array
        (
        "id" => 3,
        "parent_id" => 2,
        "name" => "Sub-Subcategory A1"
    ),
    "3" => Array
        (
        "id" => 4,
        "parent_id" => 1,
        "name" => "Subcategory A2"
    ),
    "4" => Array
        (
        "id" => 5,
        "parent_id" => 0,
        "name" => "Category B"
    ),
    "5" => Array
        (
        "id" => 6,
        "parent_id" => 5,
        "name" => "Subcategory B"
    )
);

类似的东西:

$tabOfElements = array();
foreach($array as $element){
    $tabOfElements[$element['id']] = (isset($tabOfElements[$element['parent_id']])?$tabOfElements[$element['parent_id']].'/':'').$element['name'];
}
$stringOfCategories = implode(',',$tabOfElements);

应该做你需要的。注意:仅当父类别始终位于数组中的子项之前,此方法才有效。

编辑:

要获取所有最后一个类别子路径,您可以执行以下操作:

$tabOfElements = array();
$elementsToDelete = array();
foreach($array as $element){
    $tabOfElements[$element['id']] = (isset($tabOfElements[$element['parent_id']])?$tabOfElements[$element['parent_id']].'/':'').$element['name'];       
    if(isset($tabOfElements[$element['parent_id']]) && !isset($elementsToDelete[$element['parent_id']])){
        $elementsToDelete[$element['parent_id']] = $element['parent_id'];
    }
}
$finalArray = array_diff_key($tabOfElements, $elementsToDelete);
$stringOfCategories = implode(',',$finalArray);

这会给你:

Category A/Subcategory A1/Sub-Subcategory A1,Category A/Subcategory A2,Category B/Subcategory B

编辑2: 使用你给出的第二个数组,你可以使用递归函数,如:

function recursiveFunction($elementArray, $parentString = ''){
    if(isset($elementArray['children']) && !empty($elementArray['children'])){
        foreach($elementArray['children'] as $keyChild=>$child){
            $resultString .= recursiveFunction($child, $parentString.$elementArray['name'].'/').(($keyChild<count($elementArray['children'])-1)?',':'');
        }
        return $resultString;
    }else{
        return $parentString.$elementArray['name'];
    }
}

$tabOfPaths = array();
foreach($array as $elementArray){
    $tabOfPaths[] = recursiveFunction($elementArray);
}
$stringOfPaths = implode(',',$tabOfPaths);
echo $stringOfPaths;

希望它有所帮助。