douyi1779 2012-11-01 07:08
浏览 35
已采纳

替换数组的值

php 5.2.5 I wrote the function to get modules by courseid from MySQL database.

function getModules($courses, $mod) {
global $DB;
$result = array();
foreach ($courses as $value) {
    $value->mods = array();
    $value->count = 0;
    $temp = $DB->get_records_sql("
        SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
        FROM {modules} m
        JOIN {course_modules} cm ON m.id = cm.module
        JOIN {".$mod."} q ON cm.instance = q.id
        WHERE m.name = '".$mod."' AND cm.course = ?", array($value->id)); 
    foreach ($temp as $vS) {
        $value->mods[] = $vS;
        $value->count++;
    }
    $result[] = $value;
}  
return $result;
}

Try to get some type of modules (to_debug just kind of wrapper about var_dump)

$learningScorm = getModules($learning, 'scorm');
to_debug($learningScorm);  // in debug I can see right values.
echo '<br><br><br>';
$learningLesson = getModules($learning, 'lesson');
to_debug($learningScorm);// in debug I see what value of $learningScorm is replaced by value of $learningLesson
$testingQuiz = getModules($testing, 'quiz');
$labAssignment = getModules($lab, 'assignment');

I cannot understand why this replacing is happening If you have some hints about such behaviour, please give me it.

If I comment these lines

$value->mods = array();
$value->count = 0;

then $learningScorm is summing modules from $learningScorm and $learningLesson. It seems... Seems what $courses is not local during function O_O. I do not know what to think already.

  • 写回答

2条回答 默认 最新

  • du3979 2012-11-02 08:10
    关注

    $courses is local, but objects contained by $courses are real objects. By changing $value's really items is changing too.

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

报告相同问题?

悬赏问题

  • ¥100 无网格伽辽金方法研究裂纹扩展的程序
  • ¥15 如何用数码管显示学号(相关搜索:单片机)
  • ¥15 错误于library(org.Hs.eg.db): 不存在叫‘org.Hs.eg.db’这个名称的程序包,如何解决?
  • ¥60 求一个图片处理程序,要求将图像大小跟现实生活中的大小按比例联系起来的
  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活