dream3323 2013-05-01 04:27
浏览 54
已采纳

codeigniter helper中的全局变量

im writing a recursive function that finds the children of an array if it has. now i want to know the level that an array enters finding its child. for an eg

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
                            (
                            )

                    )

            )

    )

)

and my recursive function is below and it is written in 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');
}

now i want to count the depth of the array that how much level does it went inside using the same recursive function i tried to count the level it went inside in the recursive function i.e get_children_by_par_id($parent_id). but due to recursive function count is initialized to its original value. so i need to make a global variable in the helper. so can any one help me here.... or can you give me the best idea to count the depth of the array and yes the array can go to the n level...

  • 写回答

2条回答 默认 最新

  • drl47263 2013-05-01 05:15
    关注

    This helped me out go here. I did a bit of twist and turns to the code and for further references you can Visit here. you can create a global variable in codeigniter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建