嘿,我只是停留在Array Object中。 我正在从foreach值接收数组对象中的课程列表。 这就是价值。
Cmd + Shift + T
最后,在foreach值之外,我只得到一个值。
我想要什么:
我要连接stdclass对象中的所有[fullname]值。像这样
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 </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 </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
)
代码使用:
stdClass Object
(
[fullname] => test-course-2,test-course-2,test-course-1
)
此代码仅返回一个值。请帮帮我。
答案 0 :(得分:0)
您目前每次迭代都覆盖$course
变量两次。这就是为什么您只获得一个(最后一个)值的原因。
我们需要确保将课程推入数组,而不是覆盖相同的变量。
像这样:
// 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;
以上内容将为您提供一个包含所有课程的数组,以及一个所有逗号分隔的对象。