首页 > 解决方案 > StdClass 对象只返回一个值

问题描述

嗨,我刚刚卡在数组对象中。我从 foreach 值接收数组对象中的课程列表。这就是价值。

Course List
stdClass Object
(
    [id] => 4
    [category] => 24
    [sortorder] => 10004
    [fullname] => test-course-2
    [shortname] => test-course-3
    [idnumber] => 
    [summary] => <p>Testing Course 2</p>
    [summaryformat] => 1
    [format] => singleactivity
    [showgrades] => 0
    [newsitems] => 0
    [startdate] => 0
    [enddate] => 0
    [marker] => 0
    [maxbytes] => 0
    [legacyfiles] => 0
    [showreports] => 0
    [visible] => 1
    [visibleold] => 1
    [groupmode] => 0
    [groupmodeforce] => 0
    [defaultgroupingid] => 0
    [lang] => 
    [calendartype] => 
    [theme] => 
    [timecreated] => 1536066556
    [timemodified] => 1536066556
    [requested] => 0
    [enablecompletion] => 1
    [completionnotify] => 0
    [cacherev] => 1547443283
)
Course List
stdClass Object
(
    [id] => 3
    [category] => 24
    [sortorder] => 10005
    [fullname] => test-course-2
    [shortname] => test-course-2
    [idnumber] => 
    [summary] => <p>Testing Course 2</p>
    [summaryformat] => 1
    [format] => singleactivity
    [showgrades] => 0
    [newsitems] => 0
    [startdate] => 0
    [enddate] => 0
    [marker] => 0
    [maxbytes] => 0
    [legacyfiles] => 0
    [showreports] => 0
    [visible] => 1
    [visibleold] => 1
    [groupmode] => 0
    [groupmodeforce] => 0
    [defaultgroupingid] => 0
    [lang] => 
    [calendartype] => 
    [theme] => 
    [timecreated] => 1536066548
    [timemodified] => 1536066548
    [requested] => 0
    [enablecompletion] => 1
    [completionnotify] => 0
    [cacherev] => 1547443283
)
Course List
stdClass Object
(
    [id] => 2
    [category] => 24
    [sortorder] => 10006
    [fullname] => test-course-1
    [shortname] => test-course-1
    [idnumber] => 
    [summary] => <p>Testing&nbsp;</p>
    [summaryformat] => 1
    [format] => singleactivity
    [showgrades] => 0
    [newsitems] => 0
    [startdate] => 0
    [enddate] => 0
    [marker] => 0
    [maxbytes] => 0
    [legacyfiles] => 0
    [showreports] => 0
    [visible] => 1
    [visibleold] => 1
    [groupmode] => 0
    [groupmodeforce] => 0
    [defaultgroupingid] => 0
    [lang] => 
    [calendartype] => 
    [theme] => 
    [timecreated] => 1536066531
    [timemodified] => 1536066531
    [requested] => 0
    [enablecompletion] => 1
    [completionnotify] => 0
    [cacherev] => 1547443283
)
course Receving
stdClass Object
(
    [id] => 2
    [category] => 24
    [sortorder] => 10006
    [fullname] => test-course-1
    [shortname] => test-course-1
    [idnumber] => 
    [summary] => <p>Testing&nbsp;</p>
    [summaryformat] => 1
    [format] => singleactivity
    [showgrades] => 0
    [newsitems] => 0
    [startdate] => 0
    [enddate] => 0
    [marker] => 0
    [maxbytes] => 0
    [legacyfiles] => 0
    [showreports] => 0
    [visible] => 1
    [visibleold] => 1
    [groupmode] => 0
    [groupmodeforce] => 0
    [defaultgroupingid] => 0
    [lang] => 
    [calendartype] => 
    [theme] => 
    [timecreated] => 1536066531
    [timemodified] => 1536066531
    [requested] => 0
    [enablecompletion] => 1
    [completionnotify] => 0
    [cacherev] => 1547443283
)

最后,我在 foreach 值之外只得到一个值。

我想要的是:

我想连接 stdclass 对象中的所有 [fullname] 值。像这样

stdClass Object
(
    [fullname] => test-course-2,test-course-2,test-course-1
)

代码使用:

private function get_course($course) {
    global $DB;

    /***************** CUSTOM *****************/
    //print_object($course);
    $enrolledusercourses = enrol_get_users_courses($this->user->id); 
    // print_object($enrolledusercourses);
    $cour = array();

    foreach($enrolledusercourses as $key =>$value){ 
        $course = $enrolledusercourses[$key]->id;

        /***************** CUSTOM *****************/

        if ($course) {
            // If $course is an integer, it is a course id, get the object from database.
            if (is_int($course) || is_string($course)) {
                if (!$course = $DB->get_record('course', array('id' => $course), '*', MUST_EXIST)) {
                    print_error('Course ID was incorrect');
                }
            }
        }
        echo "Course List";
        print_object($course);
    }
    echo "course Receiving";
    print_object($course);
    return $course;
}

此代码仅返回一个值。请帮帮我。

标签: php

解决方案


您当前$course在每次迭代中多次覆盖 -variable。这就是为什么你只能得到一个(最后一个)价值。

我们需要确保将课程推送到数组而不是覆盖相同的变量。
像这样的东西:

// Define the array that will keep all our courses
$courses = [];

// Define an array that will keep all the names
$names = ['fullname' => []];

foreach($enrolledusercourses as $key =>$value){ 
    // Save the ID to a temporary variable
    $id = $enrolledusercourses[$key]->id;

    if ($id) {
        // This is also a temporary variable. ($course vs $courses)
        $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);

        if ($course) {
            // We found a course, let's add it to the courses array
            $courses[] = $course;

            // Add the name to your names array
            $names['fullname'][] = $course->fullname;
        }
    }
}

// Now we have an array (the new $courses variable) that will contain all the courses.

// Implode the names
$names['fullname'] = implode(', ', $names['fullname']);
// Convert it to an object
$names = (object)$names;

echo "course Receiving";
print_object($courses);
return $courses;

以上将为您提供一个包含所有课程的数组和一个所有名称以逗号分隔的对象。


推荐阅读