dsfs504545 2015-02-23 19:33
浏览 35
已采纳

具有/ Parent ID到类别分类数组的PHP多维数组,Ebay样式

Going for days now, ready to quit. I make a call to eBay's trading API to return a list of categories. The array below shows how these categories are returned from the API. I am trying like hell to arrange it into a taxonomy like the first gray box below.

Do note that the number at the beginning of each line is the id of the final category for that particular line.

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

I have greatly shortened this array to one example. Many categories will be pumped through to build the taxonomy.

The CategoryLevel can start at any number and it can end at any number. In this example, it starts at 2 and the highest level is 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
        )


)

Thank you in advance for your help!

  • 写回答

1条回答 默认 最新

  • dsag14654 2015-02-23 20:00
    关注

    Edit: Again, fixed error.

    <?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);
    

    Outputs:

    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
    )
    

    Output is an array with the right side category id as the key and the value is a string with category text.

    This may seem like a complicated or unnecessary way to do this, but this is really one of the better ways IMO because it will handle any order for the categories and any number of nested levels. The only limit would have to do with the recursion and possibly memory but I tried to use references where possible to keep that memory footprint as small as possible.

    Example: http://codepad.viper-7.com/Fke5OA

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突
  • ¥15 如何在线硕士了解,广告太多,希望有真实接触过的人回答下?(标签-学习|关键词-在线硕士)
  • ¥15 zabbix6.4与frp如何进行联动