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...