php - 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 </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
)
最后,我在 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;
}
此代码仅返回一个值。请帮帮我。
解决方案
您当前$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;
以上将为您提供一个包含所有课程的数组和一个所有名称以逗号分隔的对象。
推荐阅读
- c++ - CreateWindowW() 第一次失败,然后工作 [C++]
- c# - 部署在 Azure 上的 Web 应用程序未返回 UTC 日期时间
- angular - Angular 中的“未知”类型上不存在属性“exhibitPhotos”
- arrays - 使用数组中的所有值执行任务(Swift、Firebase)
- php - WORDPRESS 获取 parse_request 的参数
- android-studio - 如何在 Android Studio 中完全重命名项目
- python-3.x - 无法导入scrapy
- javascript - 不允许添加定义以外的值。MongoDB
- javascript - 将音频元素与类进行比较
- python - 如何解决 Python 中的 RoboBrowser 导入错误?