PHP 多维数组 w/Parent ID 到类别分类法数组,Ebay 样式

PHP Multi-Dimensional Array w/Parent ID to Category Taxonomy Array, Ebay Style

已经走了几天了,准备辞职了。我打电话给 eBay 的交易 API 到 return 类别列表。下面的数组显示了这些类别是如何从 API 中 return 编辑的。我拼命想把它安排成一个分类法,就像下面的第一个灰色框一样。

请注意,每行开头的数字是该特定行的最终类别的 ID。

3270: Vehicle Electronics & GPS
175716: Vehicle Electronics & GPS > Car Audio
18805: Vehicle Electronics & GPS > Car Audio > Car Subwoofers
18795: Vehicle Electronics & GPS > Car Audio > Car Amplifiers
39754: Vehicle Electronics & GPS > Car Audio > Car Audio In-Dash Units

我已将此数组大大缩短为一个示例。将抽取许多类别来构建分类法。

CategoryLevel 可以从任意数字开始,也可以以任意数字结束。在这个例子中,它从 2 开始,最高级别是 4。

Array
(
    [3270] => Array
        (
            [CategoryID] => 3270
            [CategoryLevel] => 2
            [CategoryName] => Vehicle Electronics & GPS
            [CategoryParentID] => 293
        )

    [175716] => Array
        (
            [CategoryID] => 175716
            [CategoryLevel] => 3
            [CategoryName] => Car Audio
            [CategoryParentID] => 3270
        )


    [79839] => Array
        (
            [CategoryID] => 79839
            [CategoryLevel] => 4
            [CategoryName] => Signal Processors
            [CategoryParentID] => 175716
            [LeafCategory] => true
        )

    [18805] => Array
        (
            [CategoryID] => 18805
            [CategoryLevel] => 4
            [CategoryName] => Car Subwoofers
            [CategoryParentID] => 175716
            [LeafCategory] => true
        )

    [18795] => Array
        (
            [CategoryID] => 18795
            [CategoryLevel] => 4
            [CategoryName] => Car Amplifiers
            [CategoryParentID] => 175716
            [LeafCategory] => true
        )

    [39754] => Array
        (
            [CategoryID] => 39754
            [CategoryLevel] => 4
            [CategoryName] => Car Audio In-Dash Units
            [CategoryParentID] => 175716
            [LeafCategory] => true
        )


)

提前感谢您的帮助!

编辑:再次修复错误。

<?php
require_once("samplearray.php");

function makeNestedData($data){
    //create a nested tree using the above structure
    $nested = array();

    //loop over each category
    foreach($data as &$category){
        //is there is no children array, add it
        if(!isset($category['Children'])){
            $category['Children'] = array();
        }
        //check if there is a matching parent
        if(isset($data[$category['CategoryParentID']])){
            //add this under the parent as a child by reference
            if(!isset($data[$category['CategoryParentID']]['Children'])){
                $data[$category['CategoryParentID']]['Children'] = array();
            }
            $data[$category['CategoryParentID']]['Children'][$category['CategoryID']] = &$category;
        //else, no parent found, add at top level
        } else {
            $nested[$category['CategoryID']] = &$category;
        }
    }
    unset($category);
    return $nested;
}

//now flatten out the nested array recursively
function flattenNested($nested, $parent=''){
    $out = array();
    foreach($nested as $category){
        $categoryName = $parent.$category['CategoryName'];
        $out[$category['CategoryID']] = $categoryName;
        //recurse for each child
        $out += flattenNested($category['Children'], $categoryName.' > ');
    }
    return $out;
}

$result = flattenNested(makeNestedData($array));
print_r($result);

输出:

Array
(
    [3270] => Vehicle Electronics & GPS
    [175716] => Vehicle Electronics & GPS > Car Audio
    [79839] => Vehicle Electronics & GPS > Car Audio > Signal Processors
    [18805] => Vehicle Electronics & GPS > Car Audio > Car Subwoofers
    [18795] => Vehicle Electronics & GPS > Car Audio > Car Amplifiers
    [39754] => Vehicle Electronics & GPS > Car Audio > Car Audio In-Dash Units
)

输出是一个数组,右边的类别id为键,值为带有类别文本的字符串。

这似乎是一种复杂或不必要的方法,但这确实是 IMO 的更好方法之一,因为它可以处理类别的任何顺序和任何数量的嵌套级别。唯一的限制与递归和可能的内存有关,但我尝试在可能的情况下使用引用以使内存占用尽可能小。

示例:http://codepad.viper-7.com/Fke5OA