dqyz48470 2018-02-06 21:20
浏览 137
已采纳

如何使用foreach循环来回显PHP中的数组中的名字,姓氏和电子邮件地址对象以及HTML

I am new to PHP and have been struggling with echoing out a first name, last name, and email address as objects inside of a foreach loop. I have used a Profile.php class to define those and the get and set methods, and then I have a User.php class to set up a Username and Profile object and its get and set methods. I don't use the username object at all for my answer, according to people I have tried getting help from. I believe that the problem is within the foreach loop in index.php, but I have worked for hours and cannot wrap my head around it yet. Here is the UML for the program.

Here is what the program should ouput.

And here is the code itself:

index.php:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <?php
  8. require_once('User.php');
  9. require_once('Profile.php');
  10. $professors = array("Andrew Besmer", "Gerry Derksen");
  11. $andrewUser = new User();
  12. $andrew = new Profile();
  13. $andrewUser->setProfile($andrew);
  14. $andrew->setFirstName("Andrew");
  15. $andrew->setLastName("Besmer");
  16. $andrew->setEmailAddress("besmera@fakeemail.com");
  17. $gerryUser = new User();
  18. $gerry = new Profile();
  19. $gerryUser->setProfile($gerry);
  20. $gerry->setFirstName("Gerry");
  21. $gerry->setLastName("Derksen");
  22. $gerry->setEmailAddress("derkseng@fakeemail.com");
  23. foreach($professors as $profile) {
  24. echo "<h2>" . $profile . "</h2>";
  25. echo "<ul>";
  26. echo "<li><b>First:</b>" . $profile->getFirstName() . "</li>";
  27. echo "<li><b>Last:</b>" . $profile->getLastName() . "</li>";
  28. echo "<li><b>Email:</b>" . $profile->getEmailAddress() . "</li>";
  29. echo "</ul>";
  30. }
  31. echo "<br>";
  32. ?>
  33. </body>
  34. </html>

Profile.php:

  1. <?php
  2. class Profile {
  3. protected $firstName = "";
  4. protected $lastName = "";
  5. protected $emailAddress = "";
  6. public function __construct(){
  7. }
  8. public function getFirstName(){
  9. return $this->firstName;
  10. }
  11. public function setFirstName($firstName){
  12. $this->firstName = $firstName;
  13. }
  14. public function getLastName() {
  15. return $this->lastName;
  16. }
  17. public function setLastName($lastName) {
  18. $this->lastName = $lastName;
  19. }
  20. public function getEmailAddress() {
  21. return $this->emailAddress;
  22. }
  23. public function setEmailAddress($emailAddress) {
  24. $this->emailAddress = $emailAddress;
  25. }
  26. }
  27. ?>

User.php:

  1. class User {
  2. protected $username = "";
  3. protected $professors = array();
  4. public function __construct(){
  5. }
  6. public function getUsername(){
  7. return $this->username;
  8. }
  9. public function setUsername($username) {
  10. $this->username = $username;
  11. }
  12. public function getProfile(){
  13. return $this->profile;
  14. }
  15. public function setProfile($profile){
  16. $this->profile = $profile;
  17. }
  18. }

Thanks for the help and your time.

展开全部

  • 写回答

2条回答 默认 最新

  • duankang5882 2018-02-06 21:29
    关注
    $professors = array("Andrew Besmer", "Gerry Derksen");
    

    As you can see, this array contains string (the names of professers), not objects. Thus, in the foreach loop:

    foreach($professors as $profile)
    

    You will get the strings, "Andrew Besmer", and then "Gerry Derksen" as the value of $profile variable, which is not what we want.

    To solve this, you can define the $professors array after creating the objects, just before the foreach loop as follows:

    $professors  = array($andrewUser, $gerryUser);
    

    Then in the foreach loop, you will get User objects, and through which you can access Profile object:

    foreach ($professors as $user) {
        echo $user->profile->getEmailAddress();
    }
    

    Hope this will help you get started to solve your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部