How can I take the below array and make it an in-line array?

// Store user-data in array
$USER = array();

// first user info
$USER[1] = "username";
$USER[1] = "password";
$USER[1] = "rank";
$USER[1] = "session id";

 // second user info
$USER[2] = "username";
$USER[2] = "password";
$USER[2] = "rank";
$USER[2] = "session id";

// next user ...

When I say in-line I mean that I want to take the above two user arrays and put it in one line like the below model to save space:

$USER = array(

// First user
"username", "password", "rank", "session id"

// Second user
"username", "password", "rank", "session id"

);
$USER = array (
    1 => array ('username' => 'pritaeas', 'password' => 'XXX'),
    2 => array ('username' => 'Subless', 'password' => 'XXX')
);

echo $USER[1]['username'];

I have a login form and when the user clicks submit it needs to search all the $USER arrays for a match on username and password. The only problem is that my login page compares the user input like so:

if($username == $USER[???]["username"])
{
  echo 'yes';
} else
{
  echo 'no';
}

So how can I get the correct key where I have "???" before it can search the arrays for a possible match?

Member Avatar for diafol

So you want to search for a value in a multidimensional array. Slightly tricky - if it was flat you could use array_search. However, those clever people over at php manual have looked at this problem and there are a lot of solutions. Probably the easiest one I've seen is:

http://www.php.net/manual/en/function.array-search.php#92991

Alternatively, you could do a loop over the array:

$USER = array(1 => array('username'=>'diafol','password'=>'sc00byd00','rank'=>'reckless','session_id'=>'1234'), 2 => array('username'=>'ardav','password'=>'g00dis0n','rank'=>'hasbeen','session_id'=>'3456'));

$input = 'diafol';
$id = false;
foreach ($USER as $key=>$value){
    if($value['username'] == $input){
        $id = $key;
        break;
    }
}
if($id){
    echo "The id for $input is $id";    
}else{
    echo "This user doesn't exist"; 
}

It works for the first entry for admin but the guest one wont work. I had to add the if else statement to check for the correct password depending on which username was submitted. It works correctly for the "admin:hello" combo, but when I use "guest:test" combo it wont work or find a match for it. It's acting as if its not going through all the $USER arrays and just going to the first one and stopping. Here's my code I have:

<?php
// User stored information
$USER = array(
  1 => array('username' => 'admin', 'password' => 'hello'),
  2 => array('username' => 'guest', 'password' => 'test')
);

// User login input
$userName = "guest";
$userPass = "test";

$id = false;

// Check user-input against user stored information
foreach($USER as $key => $value)
{
  // Check username
  if($value['username'] == $userName)
  {
    // Username was found, store key so password can be checked for that username.
    $id = $key;

    // Check password
    if($USER[$id]['password'] == $userPass)
    {
      echo 'Username and Password matches.';
      break;
    } else
    {
      echo 'Invalid username and or password.';
      break;
    }
  } else
  {
    echo 'No match found.';
    break;
  }
}
?>
Member Avatar for diafol

OK, simple use:

$USER = array(1 => array('username'=>'diafol','password'=>'sc00byd00','rank'=>'reckless','session_id'=>'1234'), 2 => array('username'=>'ardav','password'=>'g00dis0n','rank'=>'hasbeen','session_id'=>'3456'));

$msgs = array("No matches","You are logged in","Username and password do not match");


$username = 'ardav';
$password = 'g00dis0n';

$case = 0;
foreach ($USER as $key=>$value){
    if($value['username'] == $username){
        if($value['password'] == $password){
            $case = 1;          
        }else{
            $case = 2;
        }
        break;
    }
}
echo $msgs[$case];

That still doesn't work. As I stated before it's acting like it only sees $USER[1] and not continuing to $USER[2]. I don't understand why its working like that. If you use the username: admin and the password: hello which is in $USER[1] it works fine, but if you enter in username: guest and password: test which is $USER[2] it will not work to save my life.

Member Avatar for diafol

ODD it works for me

I don't know what's going on. I've tried foreach loops, and for loops. I just don't understand why its not noticing $USER[2]. It's only picking up on $USER[1] and stopping there.

I tried out both the codes - Yours and of diafol and found out that:
It's not about the loops, it's about the ' echo "messages"; and break;'
Instead of printing the message and exitting the loop, you should store the status (of finding the match) and print the message at the end (depending on the status).
Diafol's code works fine with me too.

Ah ok that works great now that I moved the messages till the end. Thanks for the tip sasankasekhar, and thanks diafol for the help with the code!

Member Avatar for diafol

OK, is it solved?

OK, is it solved?

Yes sir Mr. diafol it's solved. I don't know why I didn't think about it, but the breaks must have been causing the loop to only run once then break out / exit.

Thank you for your great help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.