dongyaoxiu6244
2016-09-28 08:53
浏览 52
已采纳

从URL中选择PHP数组

I have coded a internal messenger system for the company I work for and I am trying to make things easier for myself by making a admin panel for this so it can control the usernames, passwords and groups.

Basically I am trying to select a user from the array using a url ?user=username which will then return the user information and allow it to be changed. The problem I am having is to get the url to select the username in the array.

The array is basic:

$users = array(
    '0' => array(
        'username' => 'test',
        'password' => 'test123',
        'group' => 'office',
    ),
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douju2474 2016-09-28 08:58
    已采纳

    Like this?:

    <?php
    
    // get username from URL (GET METHOD)
    $username=$_GET["username"];
    
    // $users has all the info
    
    foreach ($users as $u) {
      // find the target user
      if ($u["username"] == $username) {
          // do something with $u
          var_dump($u);
      }
    }
    
    ?>
    
    点赞 打赏 评论
  • dtbrd80422 2016-09-28 08:56

    Did you tried to do this:

    $user_name = $users[0]["username"];
    
    点赞 打赏 评论
  • dongni8124 2016-09-28 09:01

    Since it's an array i assume you have multiple indexes. I guess a simple 'find' function might work.

    function findUser($searchData, $users, $searchField) {
        $retObj = array();
        foreach ($users as $user) {
            if (isset($user[$searchField]) && $user[$searchField] == $searchData) {
                $retObj['username'] = $user['username'];
                $retObj['password'] = $user['password'];
                $retObj['group'] = $user['group'];
                break;
            }
        }
        return $retObj;
    }
    

    EDIT: Added searchField so u can do same for password and group. Searchdata is the value u are searching

    点赞 打赏 评论
  • dpswo40440 2016-09-28 09:03

    Try this (not tested)

    $u = null;
    foreach($user as $user) {
        if(isset($_GET['user']) && $user['username'] == $_GET['user']) {
            $u = $user;
        }
    }
    

    $u variable should contains yours user record.

    点赞 打赏 评论

相关推荐 更多相似问题