0

Can anyone tell me what am I doing wrong?

Here is my code:

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;
  }
}

I want to create methid setUser($user) where you pass in user id, and then return the users name and location.

Example of method call:

setUser(user1);

If the user is set to user1, then return user1 data, if user is set to user2, then return user2 data But the $user variable isn't set to John Doe in my example.

5
  • Where is $test passed to your function? Commented Oct 28, 2013 at 9:23
  • Yes, by nesting loops. It was typo. With $thest I ment $users array. Edited Commented Oct 28, 2013 at 9:24
  • I edited the question to bee more understandable Commented Oct 28, 2013 at 9:26
  • foreach($this->users[$user]... Commented Oct 28, 2013 at 9:26
  • Seems a pretty complicated and unmaintainable way to do things. Commented Oct 28, 2013 at 9:28

1 Answer 1

1
function setUser($user) {
  foreach ($this->users[$user] as $key => $value) {
    $this->user = $key;
    $this->location = $value;
  }
Sign up to request clarification or add additional context in comments.

Comments