我有子类别的ID,11,14,21,35,38,39
, 其中,这些都是其主要类别中的各种子类别:
父类别1
具有:11,14
父类别2
具有:21
父类别3
具有:35,38,39
我如何按照孩子的父类别对孩子ID进行分组,如下所示?
array(
1 => array ( 11,14 ),
2 => array ( 21 ),
3 => array ( 35,38,39 )
)
SO网友:Reza Mamun
我和你有完全相同的要求。下面的代码是我的解决方案:
$termArr = array();
$termIds = array_filter(explode(\',\',\'11,14,21,35,38,39\'),\'is_numeric\');
if( !empty($termIds) ){
$terms = get_terms(array(
\'taxonomy\' => \'my_taxonomy\',
\'include\' => $termIds
));
foreach($terms as $t){
if( !isset($termArr[$t->parent]) )
$termArr[$t->parent] = array();
$termArr[$t->parent][] = $t->term_id;
}
print_r($termArr); //===> This is the array which you need :)
}