首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从PHP中的字符串中获取特定数据

从PHP中的字符串中获取特定数据
EN

Stack Overflow用户
提问于 2019-08-10 18:09:11
回答 1查看 44关注 0票数 0

说明:修改下面的代码,以便打印以下内容

代码语言:javascript
复制
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)时遇到问题

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
{
  "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"
    }
  ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-10 18:16:36

我是在PHP小提琴上运行它的,这给出了错误:

代码语言:javascript
复制
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))。也就是说,对于这样一门有多位教授的课程:

代码语言:javascript
复制
{
  "academicLevel": "UG",
  "capacity": "28",
  "comments": [],
  "courseNumber": "116",
  // ..
  "instructors": [
    {
      "username": "amulahuw",
      "name": "A. Mulahuwaish"
    },
    {
      "username": "jsmith",
      "name": "J. Smith"
    },
    {
      "username": "bhappy",
      "name": "B. Happy"
    },
  ],
  // ..
}

使用此代码片段:

代码语言:javascript
复制
foreach ($obj->courses as $course) {
  echo $course->prefix . " "
     . $course->courseNumber . " "
     . $course->section . " "
     . join(',', array_map(function($instructor) {
                   return $instructor->username;
                 },
                 $course->instructors))
     . "\n" ;
}

应该给你:

代码语言:javascript
复制
CS 116 01 amukher1,jsmith,bhappy

您是在询问如何在注释中增加instructors[0]。如果有多个教授,您需要增加它,或者以其他方式遍历教授(使用array_map()join())是一种方法。但是,如果您可以假设每门课程最多有一位教授(如您的JSON示例中的那样),则不需要增加它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57444553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档