说明:修改下面的代码,使其打印以下内容
CS 116 01 amulahuw
CS 116 01 amukher1
CS 116 01 amukher1
CS 116 02 amukher1
CS 116 02 gpcorser
CS 116 02 gpcorser
我在打印用户名(amulahuw,amukher1..etc)时遇到问题
<?php
$jsonString = '{"courses":[{"academicLevel":"UG","capacity":"28","comments":[],"courseNumber":"116","credit":"4","description":"This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.","instructors":[{"username":"amulahuw","name":"A. Mulahuwaish"}],"lineNumber":"0624","location":"UC","meetingTimes":[{"method":"LEC","building":"SE","room":"135","days":"MW","startTime":"10:30 AM","endTime":"12:20 PM","dates":"08\/26\/2019-12\/07\/2019","instructor":"amulahuw"},{"method":"FNL","building":"SE","room":"135","days":"M","startTime":"10:30 AM","endTime":"12:20 PM","dates":"12\/08\/2019-12\/14\/2019"}],"prefix":"CS","prerequisites":"MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ","seatsAvailable":"4","section":"01","status":"Open","term":"19\/FA","title":"Computer Programming I"},{"academicLevel":"UG","capacity":"30","comments":[],"courseNumber":"116","credit":"4","description":"This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.","instructors":[{"username":"amukher1","name":"A. Mukherjee"}],"lineNumber":"0610","location":"UC","meetingTimes":[{"method":"LEC","building":"SE","room":"145","days":"TR","startTime":"08:30 AM","endTime":"10:20 AM","dates":"01\/14\/2019-04\/27\/2019","instructor":"amukher1"},{"method":"FNL","building":"SE","room":"145","days":"T","startTime":"08:30 AM","endTime":"10:20 AM","dates":"04\/29\/2019-05\/04\/2019"}],"prefix":"CS","prerequisites":"MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ","seatsAvailable":"7","section":"01","status":"Open","term":"19\/WI","title":"Computer Programming I"},{"academicLevel":"UG","capacity":"29","comments":[],"courseNumber":"116","credit":"4","description":"This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.","instructors":[{"username":"amukher1","name":"A. Mukherjee"}],"lineNumber":"0590","location":"UC","meetingTimes":[{"method":"LEC","building":"SE","room":"145","days":"TR","startTime":"08:30 AM","endTime":"10:20 AM","dates":"01\/13\/2020-05\/02\/2020","instructor":"amukher1"}],"prefix":"CS","prerequisites":"MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ","seatsAvailable":"4","section":"01","status":"Open","term":"20\/WI","title":"Computer Programming I"},{"academicLevel":"UG","capacity":"28","comments":[],"courseNumber":"116","credit":"4","description":"This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.","instructors":[{"username":"amukher1","name":"A. Mukherjee"}],"lineNumber":"0626","location":"UC","meetingTimes":[{"method":"LEC","building":"SE","room":"145","days":"TR","startTime":"08:30 AM","endTime":"10:20 AM","dates":"08\/26\/2019-12\/07\/2019","instructor":"amukher1"},{"method":"FNL","building":"SE","room":"145","days":"T","startTime":"08:30 AM","endTime":"10:20 AM","dates":"12\/08\/2019-12\/14\/2019"}],"prefix":"CS","prerequisites":"MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ","seatsAvailable":"10","section":"02","status":"Open","term":"19\/FA","title":"Computer Programming I"},{"academicLevel":"UG","capacity":"30","comments":[],"courseNumber":"116","credit":"4","description":"This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.","instructors":[{"username":"gpcorser","name":"G. Corser"}],"lineNumber":"0612","location":"UC","meetingTimes":[{"method":"LEC","building":"SE","room":"145","days":"MW","startTime":"12:30 PM","endTime":"02:20 PM","dates":"01\/14\/2019-04\/27\/2019","instructor":"gpcorser"},{"method":"FNL","building":"SE","room":"145","days":"M","startTime":"12:30 PM","endTime":"02:20 PM","dates":"04\/29\/2019-05\/04\/2019"}],"prefix":"CS","prerequisites":"MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ","seatsAvailable":"5","section":"02","status":"Open","term":"19\/WI","title":"Computer Programming I"},{"academicLevel":"UG","capacity":"29","comments":[],"courseNumber":"116","credit":"4","description":"This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.","instructors":[{"username":"gpcorser","name":"G. Corser"}],"lineNumber":"0592","location":"UC","meetingTimes":[{"method":"LEC","building":"SE","room":"145","days":"MW","startTime":"12:30 PM","endTime":"02:20 PM","dates":"01\/13\/2020-05\/02\/2020","instructor":"gpcorser"}],"prefix":"CS","prerequisites":"MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ","seatsAvailable":"0","section":"02","status":"Clsd","term":"20\/WI","title":"Computer Programming I"}]}';
$obj = json_decode($jsonString);
foreach ($obj->courses as $course) {
echo $course->prefix . " " . $course->courseNumber . " " . $course->section . " " . $course->username . "\n" ;
}
我希望它能为我提供数据中的用户名,但是却没有错误消息,它什么也没有给我。
美化JSON:
{
"courses": [
{
"academicLevel": "UG",
"capacity": "28",
"comments": [],
"courseNumber": "116",
"credit": "4",
"description": "This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.",
"instructors": [
{
"username": "amulahuw",
"name": "A. Mulahuwaish"
}
],
"lineNumber": "0624",
"location": "UC",
"meetingTimes": [
{
"method": "LEC",
"building": "SE",
"room": "135",
"days": "MW",
"startTime": "10:30 AM",
"endTime": "12:20 PM",
"dates": "08/26/2019-12/07/2019",
"instructor": "amulahuw"
},
{
"method": "FNL",
"building": "SE",
"room": "135",
"days": "M",
"startTime": "10:30 AM",
"endTime": "12:20 PM",
"dates": "12/08/2019-12/14/2019"
}
],
"prefix": "CS",
"prerequisites": "MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ",
"seatsAvailable": "4",
"section": "01",
"status": "Open",
"term": "19/FA",
"title": "Computer Programming I"
},
{
"academicLevel": "UG",
"capacity": "30",
"comments": [],
"courseNumber": "116",
"credit": "4",
"description": "This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.",
"instructors": [
{
"username": "amukher1",
"name": "A. Mukherjee"
}
],
"lineNumber": "0610",
"location": "UC",
"meetingTimes": [
{
"method": "LEC",
"building": "SE",
"room": "145",
"days": "TR",
"startTime": "08:30 AM",
"endTime": "10:20 AM",
"dates": "01/14/2019-04/27/2019",
"instructor": "amukher1"
},
{
"method": "FNL",
"building": "SE",
"room": "145",
"days": "T",
"startTime": "08:30 AM",
"endTime": "10:20 AM",
"dates": "04/29/2019-05/04/2019"
}
],
"prefix": "CS",
"prerequisites": "MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ",
"seatsAvailable": "7",
"section": "01",
"status": "Open",
"term": "19/WI",
"title": "Computer Programming I"
},
{
"academicLevel": "UG",
"capacity": "29",
"comments": [],
"courseNumber": "116",
"credit": "4",
"description": "This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.",
"instructors": [
{
"username": "amukher1",
"name": "A. Mukherjee"
}
],
"lineNumber": "0590",
"location": "UC",
"meetingTimes": [
{
"method": "LEC",
"building": "SE",
"room": "145",
"days": "TR",
"startTime": "08:30 AM",
"endTime": "10:20 AM",
"dates": "01/13/2020-05/02/2020",
"instructor": "amukher1"
}
],
"prefix": "CS",
"prerequisites": "MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ",
"seatsAvailable": "4",
"section": "01",
"status": "Open",
"term": "20/WI",
"title": "Computer Programming I"
},
{
"academicLevel": "UG",
"capacity": "28",
"comments": [],
"courseNumber": "116",
"credit": "4",
"description": "This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.",
"instructors": [
{
"username": "amukher1",
"name": "A. Mukherjee"
}
],
"lineNumber": "0626",
"location": "UC",
"meetingTimes": [
{
"method": "LEC",
"building": "SE",
"room": "145",
"days": "TR",
"startTime": "08:30 AM",
"endTime": "10:20 AM",
"dates": "08/26/2019-12/07/2019",
"instructor": "amukher1"
},
{
"method": "FNL",
"building": "SE",
"room": "145",
"days": "T",
"startTime": "08:30 AM",
"endTime": "10:20 AM",
"dates": "12/08/2019-12/14/2019"
}
],
"prefix": "CS",
"prerequisites": "MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ",
"seatsAvailable": "10",
"section": "02",
"status": "Open",
"term": "19/FA",
"title": "Computer Programming I"
},
{
"academicLevel": "UG",
"capacity": "30",
"comments": [],
"courseNumber": "116",
"credit": "4",
"description": "This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.",
"instructors": [
{
"username": "gpcorser",
"name": "G. Corser"
}
],
"lineNumber": "0612",
"location": "UC",
"meetingTimes": [
{
"method": "LEC",
"building": "SE",
"room": "145",
"days": "MW",
"startTime": "12:30 PM",
"endTime": "02:20 PM",
"dates": "01/14/2019-04/27/2019",
"instructor": "gpcorser"
},
{
"method": "FNL",
"building": "SE",
"room": "145",
"days": "M",
"startTime": "12:30 PM",
"endTime": "02:20 PM",
"dates": "04/29/2019-05/04/2019"
}
],
"prefix": "CS",
"prerequisites": "MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ",
"seatsAvailable": "5",
"section": "02",
"status": "Open",
"term": "19/WI",
"title": "Computer Programming I"
},
{
"academicLevel": "UG",
"capacity": "29",
"comments": [],
"courseNumber": "116",
"credit": "4",
"description": "This course presents an introduction to the concepts and techniques of computer programming. Emphasis is placed on developing the students ability to apply problem-solving strategies to design algorithms and to implement these algorithms in the Java Programming language.",
"instructors": [
{
"username": "gpcorser",
"name": "G. Corser"
}
],
"lineNumber": "0592",
"location": "UC",
"meetingTimes": [
{
"method": "LEC",
"building": "SE",
"room": "145",
"days": "MW",
"startTime": "12:30 PM",
"endTime": "02:20 PM",
"dates": "01/13/2020-05/02/2020",
"instructor": "gpcorser"
}
],
"prefix": "CS",
"prerequisites": "MATH 103 or equivalent (Previous, Required) CS 105 (Previous, Required) ",
"seatsAvailable": "0",
"section": "02",
"status": "Clsd",
"term": "20/WI",
"title": "Computer Programming I"
}
]
}
答案 0 :(得分:0)
我在PHP小提琴上运行它,它给出了错误:
E_NOTICE : type 8 -- Undefined property: stdClass::$username -- at line 7
CS 116 01
E_NOTICE : type 8 -- Undefined property: stdClass::$username -- at line 7
CS 116 01
E_NOTICE : type 8 -- Undefined property: stdClass::$username -- at line 7
CS 116 01
E_NOTICE : type 8 -- Undefined property: stdClass::$username -- at line 7
CS 116 02
E_NOTICE : type 8 -- Undefined property: stdClass::$username -- at line 7
CS 116 02
E_NOTICE : type 8 -- Undefined property: stdClass::$username -- at line 7
CS 116 02
我不太确定您为什么没有收到这些错误,并且我对PHP不太熟悉-也许检查您的php.ini文件以关闭错误报告设置?
username
不是课程的属性,而是课程讲师的属性。似乎每门课程只有一名讲师,因此您可以将$course->username
替换为$course->instructors[0]->username
。
当然,如果您希望使用多个用户名,则应遍历它们或以某种方式加入它们,例如使用join(',', array_map(function(instructor) { return instructor->username }, $course->instructors))
。即,针对由多位教授组成的此类课程:
{
"academicLevel": "UG",
"capacity": "28",
"comments": [],
"courseNumber": "116",
// ..
"instructors": [
{
"username": "amulahuw",
"name": "A. Mulahuwaish"
},
{
"username": "jsmith",
"name": "J. Smith"
},
{
"username": "bhappy",
"name": "B. Happy"
},
],
// ..
}
使用以下代码段:
foreach ($obj->courses as $course) {
echo $course->prefix . " "
. $course->courseNumber . " "
. $course->section . " "
. join(',', array_map(function($instructor) {
return $instructor->username;
},
$course->instructors))
. "\n" ;
}
应该给你:
CS 116 01 amukher1,jsmith,bhappy
您在询问要增加评论中的instructors[0]
。如果有多位教授,则需要增加教授人数,否则循环遍历这些教授(使用array_map()
和join()
)是一种方法。但是,如果您假设每门课程最多教授一位教授(如您的JSON示例中所示),则无需增加它。