I have a function checkLevel($upper,$lower)
. Suppose I pass checkLevel(2,4)
. Now 2 is the parent of 3 and 3 is the parent of 4. The gap between them is of 2. Similarly, if I enter checkLevel(1,4)
, the result should be 3 because 1 is the parent of 2, 2 is the parent of 3 and 3 is the parent of 4. The number of levels are 3. Please see the image below. Can any one help me out?
It should work.
$level = 0;
function checkLevel($start,$end)
{
global $level;
$level++;
$array = array(0=>array('id'=>1,'parent_id'=>'0','test'=>'a'),1=>array('id'=>2,'parent_id'=>'1','test'=>'b'),2=>array('id'=>3,'parent_id'=>'2','test'=>'c'),3=>array('id'=>4,'parent_id'=>'3','test'=>'d'));
$key = array_search($end,array_column($array, 'id'));
if($key == $start)
return;
else
checkLevel($start,$key);
//echo $key;exit;
}
checkLevel(2,4);
echo $level;