$USER = array (
1 => array ('username' => 'pritaeas', 'password' => 'XXX'),
2 => array ('username' => 'Subless', 'password' => 'XXX')
);
echo $USER[1]['username'];
pritaeas
Posting Prodigy
9,308 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,463
Skill Endorsements: 86
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";
}
diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
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];
diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
Question Answered as of 11 Months Ago by
diafol,
pritaeas
and
sasankasekhar diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57