douyi1779 2012-10-31 23: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 00:10
    关注

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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部