1

我正在编写一个递归函数,如果有的话,它会找到数组的子元素。现在我想知道一个数组进入找到它的孩子的级别。例如

Array
(
[0] => stdClass Object
    (
        [fld_id] => 7
        [fld_value] => Color
        [fld_price] => 0.00
        [fld_attribute_id] => 2
        [fld_parent_id] => 5
        [children] => Array
            (
                [0] => stdClass Object
                    (
                        [fld_id] => 8
                        [fld_value] => Red
                        [fld_price] => 12.00
                        [fld_attribute_id] => 2
                        [fld_parent_id] => 7
                        [children] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [fld_id] => 10
                                        [fld_value] => light red
                                        [fld_price] => 20.00
                                        [fld_attribute_id] => 2
                                        [fld_parent_id] => 8
                                        [children] => Array
                                            (
                                            )

                                    )

                                [1] => stdClass Object
                                    (
                                        [fld_id] => 11
                                        [fld_value] => dark red
                                        [fld_price] => 4.00
                                        [fld_attribute_id] => 2
                                        [fld_parent_id] => 8
                                        [children] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                        [fld_id] => 14
                                                        [fld_value] => double_dark
                                                        [fld_price] => 3.00
                                                        [fld_attribute_id] => 2
                                                        [fld_parent_id] => 11
                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                                [1] => stdClass Object
                                                    (
                                                        [fld_id] => 15
                                                        [fld_value] => single_dark
                                                        [fld_price] => 0.00
                                                        [fld_attribute_id] => 2
                                                        [fld_parent_id] => 11
                                                        [children] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

                [1] => stdClass Object
                    (
                        [fld_id] => 9
                        [fld_value] => Green
                        [fld_price] => 5.00
                        [fld_attribute_id] => 2
                        [fld_parent_id] => 7
                        [children] => Array
                            (
                            )

                    )

            )

    )

)

我的递归函数在下面,它是用codeigniter helper编写的

function get_children_by_par_id($parent_id)
{
$children = get_children($parent_id);
$return_value = array();
foreach($children->result() as $result)
{
    $result->children = get_children_by_par_id($result->fld_id);
    $return_value[]= $result;
}
return ($return_value); 
}

function get_children($id){
    $CI = get_instance();
    $CI->db->where('fld_parent_id',$id);
    return $CI->db->get('tbl_attribute_values');
}

现在我想计算数组的深度,它使用相同的递归函数进入了多少级别,我试图计算它在递归函数中进入的级别,即get_children_by_par_id($parent_id)。但由于递归函数计数被初始化为其原始值。所以我需要在助手中创建一个全局变量。所以任何人都可以在这里帮助我....

4

2 回答 2

0

这帮助我走出了这里。我对代码做了一些曲折,您可以访问此处以获取更多参考。您可以在 codeigniter 中创建一个全局变量。

于 2013-05-01T05:15:20.983 回答
0

您可以在类中声明一个用于计算级别的变量,然后使用它来计算您的函数中的级别get_children_by_par_id

正如您在中看到的,test_function您可以$count在调用函数后获取变量的值,然后如果需要再次调用该函数,则必须重置它。

class yourClass extends somthing{
        $count = 0;
        function get_children_by_par_id($parent_id){
           $children = get_children($parent_id);
           $return_value = array();
           foreach($children->result() as $result){
               $result->children = get_children_by_par_id($result->fld_id);
               $this->count++;
               $return_value[]= $result;
           }
           return ($return_value); 
        }

        function get_children($id){
             $CI = get_instance();
             $CI->db->where('fld_parent_id',$id);
             return $CI->db->get('tbl_attribute_values');
        }
        function test_function(){
             $childs = $this->get_children_by_par_id(1);
             $childs_count = $this->count; // get the levels count
             $this->count = 0; //reset the counter
        }

    }
于 2013-05-01T05:35:01.040 回答