如何使用foreach循环与PHP和HTML一起在数组中的阵列中呼应名字,姓氏和电子邮件地址对象



我是PHP的新手,并且一直在努力回荡一个名字,姓氏和电子邮件地址作为foreach循环内部的对象。我已经使用了一个profile.php类来定义这些和get和设置方法,然后我有一个user.php类来设置用户名和配置文件对象及其get和设置方法。根据我尝试从中获得帮助的人,我根本不使用用户名对象作为答案。我相信这个问题在index.php中的foreach循环中,但是我已经工作了几个小时,但还无法缠绕着我的头。这是该程序的UML。

这是该程序应该输出的。

这是代码本身:

index.php:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <?php
        require_once('User.php');
        require_once('Profile.php');
        $professors = array("Andrew Besmer", "Gerry Derksen");
        $andrewUser = new User();
        $andrew = new Profile();
        $andrewUser->setProfile($andrew);
        $andrew->setFirstName("Andrew");
        $andrew->setLastName("Besmer");
        $andrew->setEmailAddress("besmera@fakeemail.com");
        $gerryUser = new User();
        $gerry = new Profile();
        $gerryUser->setProfile($gerry);
        $gerry->setFirstName("Gerry");
        $gerry->setLastName("Derksen");
        $gerry->setEmailAddress("derkseng@fakeemail.com");
        foreach($professors as $profile) {
            echo "<h2>" . $profile . "</h2>";
            echo "<ul>";
                echo "<li><b>First:</b>" . $profile->getFirstName() . "</li>";
                echo "<li><b>Last:</b>" . $profile->getLastName() . "</li>";
                echo "<li><b>Email:</b>" . $profile->getEmailAddress() . "</li>";
            echo "</ul>";
        }
        echo "<br>";
        ?>
    </body>
</html>

profile.php:

<?php
class Profile { 
    protected $firstName = "";
    protected $lastName = "";
    protected $emailAddress = "";
    public function __construct(){
    }
    public function getFirstName(){
        return $this->firstName;
    }
    public function setFirstName($firstName){
        $this->firstName = $firstName;
    }
    public function getLastName() {
        return $this->lastName;
    }
    public function setLastName($lastName) {
        $this->lastName = $lastName;
    }
    public function getEmailAddress() {
        return $this->emailAddress;
    }
    public function setEmailAddress($emailAddress) {
        $this->emailAddress = $emailAddress;
    }
}
?>

user.php:

class User {
protected $username = "";
protected $professors = array();
public function __construct(){
}
public function getUsername(){
    return $this->username;
}
public function setUsername($username) {
    $this->username = $username;
}
public function getProfile(){
    return $this->profile;
}
public function setProfile($profile){
    $this->profile = $profile;
  }
}

感谢您的帮助和您的时间。

$professors = array("Andrew Besmer", "Gerry Derksen");

您可以看到,此数组包含string(教授的名称(,而不是对象。因此,在foreach循环中:

foreach($professors as $profile)

您将获得字符串,"Andrew Besmer",然后将"Gerry Derksen"作为$profile变量的值,这不是我们想要的。

要解决此问题,您可以在创建对象之后定义$professors数组,在foreach循环之前如下:

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

然后在foreach循环中,您将获得User对象,并且您可以访问Profile对象:

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

希望这将帮助您开始解决问题。

而不是

 $professors = array("Andrew Besmer", "Gerry Derksen");

尝试使用

$professors[] = $andrewUser;
$professors[] = $gerryUser;
foreach ($professors as $professor) {
  echo "<h2>" . $professor->profile->getFirstName() . "</h2>";
  etc...
}

最新更新