首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP多维数组w/父ID到类别分类数组,Ebay风格

PHP多维数组w/父ID到类别分类数组,Ebay风格
EN

Stack Overflow用户
提问于 2015-02-23 19:33:27
回答 1查看 352关注 0票数 3

几天后准备辞职。我给eBay的交易API打了个电话,以返回一个类别列表。下面的数组显示了如何从API返回这些类别。我正拼命地把它整理成一个分类法,就像下面的第一个灰色盒子。

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

代码语言:javascript
复制
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。

代码语言:javascript
复制
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
        )


)

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-23 20:00:00

编辑:再次,修正错误。

代码语言:javascript
复制
<?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);

产出:

代码语言:javascript
复制
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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28681875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档