标签:ebay multidimensional-array iteration arrays php
现在要待几天,准备退出.我调用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
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。