从和数组中递归查找所有子类别 - PHP
Find all Child categories recursively from and Array - PHP
我像这样从数据库中获取了所有类别。
array
11 =>
object(stdClass)[19]
public 'inv_category_id' => string '11' (length=2)
public 'parent_category_id' => string '0' (length=2)
public 'name' => string 'Innerwear' (length=9)
public 'level' => string '3' (length=1)
40 =>
object(stdClass)[20]
public 'inv_category_id' => string '40' (length=2)
public 'parent_category_id' => string '11' (length=2)
public 'name' => string 'Dupattas, Scarves & Stoles' (length=26)
public 'level' => string '3' (length=1)
38 =>
object(stdClass)[21]
public 'inv_category_id' => string '38' (length=2)
public 'parent_category_id' => string '11' (length=2)
public 'name' => string 'Athletic Apparel' (length=16)
public 'level' => string '3' (length=1)
37 =>
object(stdClass)[22]
public 'inv_category_id' => string '37' (length=2)
public 'parent_category_id' => string '38' (length=2)
public 'name' => string 'Sweaters' (length=8)
public 'level' => string '3' (length=1)
我有这个功能来查找类别 ID 11 的所有子类别
public function get_all_child_categories($cat_id) {
global $all_categories_details;
$all_child = array();
foreach ($all_categories_details as $cat){
if($cat->parent_category_id == $cat_id){
$all_child[] = $cat->inv_category_id;
return self::get_all_child_categories($cat->inv_category_id);
}
}
return $all_child;
}
我是这样调用这个函数的get_all_child_categories($parent_cat_id)(11);
我试过了
$all_child = self::get_all_child_categories($cat->inv_category_id);
和
$all_child[] = self::get_all_child_categories($cat->inv_category_id);
但它 returns 是一个空数组。
我不知道我哪里错了。
试试这个
public function get_all_child_categories($cat_id, $all_child = array()) {
global $all_categories_details;
foreach ($all_categories_details as $cat){
if($cat->parent_category_id == $cat_id){
$all_child[] = $cat->inv_category_id;
$all_child = self::get_all_child_categories($cat->inv_category_id, $all_child);
}
}
return $all_child;
}
我像这样从数据库中获取了所有类别。
array
11 =>
object(stdClass)[19]
public 'inv_category_id' => string '11' (length=2)
public 'parent_category_id' => string '0' (length=2)
public 'name' => string 'Innerwear' (length=9)
public 'level' => string '3' (length=1)
40 =>
object(stdClass)[20]
public 'inv_category_id' => string '40' (length=2)
public 'parent_category_id' => string '11' (length=2)
public 'name' => string 'Dupattas, Scarves & Stoles' (length=26)
public 'level' => string '3' (length=1)
38 =>
object(stdClass)[21]
public 'inv_category_id' => string '38' (length=2)
public 'parent_category_id' => string '11' (length=2)
public 'name' => string 'Athletic Apparel' (length=16)
public 'level' => string '3' (length=1)
37 =>
object(stdClass)[22]
public 'inv_category_id' => string '37' (length=2)
public 'parent_category_id' => string '38' (length=2)
public 'name' => string 'Sweaters' (length=8)
public 'level' => string '3' (length=1)
我有这个功能来查找类别 ID 11 的所有子类别
public function get_all_child_categories($cat_id) {
global $all_categories_details;
$all_child = array();
foreach ($all_categories_details as $cat){
if($cat->parent_category_id == $cat_id){
$all_child[] = $cat->inv_category_id;
return self::get_all_child_categories($cat->inv_category_id);
}
}
return $all_child;
}
我是这样调用这个函数的get_all_child_categories($parent_cat_id)(11);
我试过了
$all_child = self::get_all_child_categories($cat->inv_category_id);
和
$all_child[] = self::get_all_child_categories($cat->inv_category_id);
但它 returns 是一个空数组。
我不知道我哪里错了。
试试这个
public function get_all_child_categories($cat_id, $all_child = array()) {
global $all_categories_details;
foreach ($all_categories_details as $cat){
if($cat->parent_category_id == $cat_id){
$all_child[] = $cat->inv_category_id;
$all_child = self::get_all_child_categories($cat->inv_category_id, $all_child);
}
}
return $all_child;
}