php类型分类样式,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

)

)

预先感谢您的帮助!

解决方法:

编辑:再次,修复错误.

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更好的方法之一,因为它将处理类别的任何顺序和任何数量的嵌套级别.唯一的限制将与递归和可能的内存有关,但是我尝试在可能的地方使用引用,以使内存占用空间尽可能小.

标签:ebay,multidimensional-array,iteration,arrays,php

来源: https://codeday.me/bug/20191120/2045898.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值