我有一个包含类别和子类别的数组。我需要安排这个,以便父级的'next_id'包含其子类别的id,然后移动到下一个父类别。 请帮忙。
$master=array
(
'97' => array
(
'name' => '★ CLOTHING',
'parent' => 0,
'level' => 0,
'path' => 97,
'next_id' => 51
),
'51'=> array
(
'name' => '★ GO GIRL +',
'parent' => 0,
'level' => 0,
'path' => 51,
'next_id' => 126,
),
'98' => array
(
'name' => '★ SARONGS & KAFTANS',
'parent' => 0,
'level' => 0,
'path' => 98,
'next_id' => 129
),
'129' => array
(
'name' => '★ SHAPEWEAR',
'parent' => 0,
'level' => 0,
'path' => 129,
'next_id' => 127
),
'127'=> array
(
'name' => '--------------------------------',
'parent' => 0,
'level' => 0,
'path' => 127,
'next_id' => 38
),
'87' => array
(
'name' => '★ FOOTWEAR',
'parent' => 0,
'level' => 0,
'path' => 87,
'next_id' => 119
),
'119' => array
(
'name' => '☆ BOTTOMS',
'parent' => 97,
'level' => 1,
'path' => '97_119',
'next_id' => 99
),
'99' => array
(
'name' => '☆ KAFTANS',
'parent' => 98,
'level' => 1,
'path' => '98_99',
'next_id' => 139
),
'139' => array
(
'name' => 'IPANEMA',
'parent' => 87,
'level' => 1,
'path' => '87_139',
'next_id' => 132
),
'132' => array
(
'name' => 'RENIU LOTIONS - Retail Only',
'parent' => 38,
'level' => 1,
'path' => '38_132',
'next_id' => 133
),
'133' => Array
(
'name' => 'SUNGLASSES - Retail Only',
'parent' => 38,
'level' => 1,
'path' => '38_133',
'next_id' => 140
),
'140' => array
(
'name' => 'THERAPY',
'parent' => 87,
'level' => 1,
'path' => '87_140',
'next_id' => 131
),
'131' => array
(
'name' => 'WATCHES - Retail Only',
'parent' => 38,
'level' => 1,
'path' => '38_131',
'next_id' => 85
),
'85' => array
(
'name' => '☆ ALL ABOUT EVE - Retail',
'parent' => 100,
'level' => 1,
'path' => '100_85',
'next_id' => 102
),
'102' => array
(
'name' => '☆ ONE SUMMER - Retail',
'parent' => 100,
'level' => 1,
'path' => '100_102',
'next_id' => 39
),
'39' => array
(
'name' => '☆ TOPS',
'parent' => 97,
'level' => 1,
'path' => '97_39',
'next_id' => 84
),
'84' => array
(
'name' => 'KEY CHARMS',
'parent' => 38,
'level' => 1,
'path' => '38_84',
'next_id' => 69
),
'69' => array
(
'name' => 'RINGS',
'parent' => 117,
'level' => 1,
'path' => '117_69',
'next_id' => 42
),
'42' => array
(
'name' => 'SCARVES',
'parent' => 38,
'level' => 1,
'path' => '38_42',
'next_id' => 31
),
'31' => array
(
'name' => '☆ DRESSES',
'parent' => 97,
'level' => 1,
'path' => '97_31',
'next_id' => 94
),
'94' => array
(
'name' => '☆ PLAYSUITS',
'parent' => 97,
'level' => 1,
'path' => '97_94',
'next_id' => 118
),
'118' => array
(
'name' => 'BELTS',
'parent' => 38,
'level' => 1,
'path' => '38_118',
'next_id' => 50
),
'50' => array
(
'name' => '☆ SARONGS',
'parent' => 98,
'level' => 1,
'path' => '98_50',
'next_id' => 26
),
'26' => array
(
'name' => 'EARRINGS',
'parent' => 117,
'level' => 1,
'path' => '117_26',
'next_id' => 40
),
);
答案 0 :(得分:0)
像你那样的扁平结构也不错。我会尝试更改我的代码以适应数据,而不是更改数据以适应代码。
您可以执行以下操作:
foreach($master as $id => $category)
if($category['parent'] != 0 && isset($master[$category['parent']]['children']))
$master[$category['parent']]['children'][$id] = $id;
else
$master[$category['parent']]['children'] = array($id => $id);
// $id => $id automatically remove possible duplicates use array($id) and [] if you don't want/need this.
所以你有一个从你父母到孩子的链接。然后,您可以使用递归函数来处理数据。
function display($id)
{
global $master;
// output element
if(isset($master[$id]['children'])) foreach($master[$id]['children'] as $child)
display($child);
}
如果您需要预先订购,请尝试以下方式:
function display($id)
{
global $master;
// output element
$next_id = reset($master[$id]['children']); // i don't know how to better identify the first child
while($next_id != 0){
display($next_id);
$next_id = $master[$next_id]['next_id'];
}
if($master[$id]['next_id'] != 0)
display($master[$id]['next_id']);
}
我认为最后一个是你想要的。 我希望这可以让你了解我的意思。