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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么