谁能告诉我我做错了什么?
这是我的代码:
private $user;
private $location;
private $users = array('user1' => array('John Doe' => 'NYC'),
'user2' => array('Jane Doe' => 'NYC'));
function setUser($user) {
foreach ($users[$user] as $key => $value) {
$this->user = $key;
$this->location = $value;
}
}
我想创建传递用户ID的methid setUser($user)
,然后返回用户名和位置。
方法调用示例:
setUser(user1);
如果用户设置为user1,则返回user1数据,如果user设置为user2,则返回user2数据 但在我的示例中,$ user变量未设置为John Doe。
答案 0 :(得分:1)
function setUser($user) {
foreach ($this->users[$user] as $key => $value) {
$this->user = $key;
$this->location = $value;
}