I have a variable which is an argument of a recursive function, and its value change in the code without warning. I echo $idParent
before and in a for
, and the value is different.
public function sortCategoriesByLevel($categories, $levelMax, $idParent, $currentLevel = 0) {
$sortedCategories = array();
$categoriesFiltered = array();
//here is what i don't understand :
echo ' idparent sent to fonction : '.$idParent; //output the id
for($i = 0; $i < count($categories); $i++) {
echo '<br /> idParent in "for" : ';
var_dump($idParent); //output -1 no matter what!!!!
echo '<br /> isset idparent : ';
echo isset($idParent);
$level = $categories[$i]->level;
echo '<br /> RUN <br />';
if($level == $currentLevel) {
echo '<br /> TRAITEMENT <br />';
$id = $categories[$i]->id;
echo 'idparent before condition : '.$idParent.' id : '.$id;
if($idParent == $id || $idParent == -1) {
$sortedCategories[$currentLevel][$categories[$i]->id][0] = $categories[$i];
echo '<br /> ENREGISTREMENT <br />';
} else {
echo '<br /> SAUVEGARDE <br />';
$categoriesFiltered[] = $categories[$i];
}
/*echo '<br />';
var_dump($categoriesFiltered);
echo '<br />';*/
if($currentLevel < $levelMax) {
echo '<br /> APPEL <br />';
$currentLevel++;
$sortedCategories[$currentLevel][$categories[$i]->id][1][] = $this->sortCategoriesByLevel($categoriesFiltered, $levelMax, $id, $currentLevel);
}
}
}
return $sortedCategories;
}
The result is:
idparent sent to fonction : -1
idParent in "for" : int(-1)
isset idparent : 1
RUN
TRAITEMENT
idparent before condition : -1 id : 18
ENREGISTREMENT
APPEL
idparent sent to fonction : 18
idParent in "for" : int(-1)
isset idparent : 1
RUN
I have put in bold the part I don't understand.