douti9253 2014-10-17 14:56
浏览 41

for循环中的变量内部递归函数更改

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 matlab有关常微分方程的问题求解决
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考