编程语言
首页 > 编程语言> > PHP多维数组,带有父ID至类别分类数组,Ebay样式

PHP多维数组,带有父ID至类别分类数组,Ebay样式

作者:互联网

现在要待几天,准备退出.我调用eBay的交易API以返回类别列表.下面的数组显示了如何从API返回这些类别.我正在尝试像地狱一样将其分类,如下面的第一个灰色框所示.

请注意,每行开头的数字是该特定行的最终类别的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

标签:ebay,multidimensional-array,iteration,arrays,php
来源: https://codeday.me/bug/20191120/2045898.html