0

How would I get a value from two arrays at a specific index? I have a $usernames array, and a $passwords array. I also have variables called $username and $password (which are the username and password user entered).

I want to get the index of $username in $usernames and compare it to the index of $password in $passwords. If they match, username and password are correct. If not, they are not correct.

I know array's are probably not the best way to do it, but its only for like 5 people and does not really have to be top secret, just a simple password protect.

I have tried:

<?php
$usernames = array("test1", "test2");
$passwords = array("password1", "password2");
$list = array($usernames, $passwords);
if (in_array($userName, $usernames)) {
   $userNameIndex = returnIndex($usernames, $userName);   
   $passWordIndex = returnIndex($passwords, $password);   
   echo("it says:<br />");
   echo($userNameIndex . " and password: " . $passWordIndex);
} else {
  echo("Not In Array");
}
?>

<?php
function returnIndex($array, $value) {
    $ar = $array;

    $searchValue = $value;

    for($i=0; $i< count($ar); $i++) {
        if($ar[i] == $searchValue) return i;
    }
}
?>

However it returns

it says:

and password: i

2
  • Save yourself a lot of work and use an associative array instead: $users = array("user1" => "password1", "user2" => "password2"); then check with just if ($users[$username] == $password) { .... Commented Mar 24, 2012 at 18:11
  • 1
    Tip: In if($ar[i] == $searchValue) return i; replace i with $i to make it valid PHP code. Watch your errors, enable warnings. Commented Mar 24, 2012 at 18:11

4 Answers 4

3

First of all, you don't need a function returnIndex because PHP already has one: it's called array_search.

You would use it like this:

$usernames = array("test1", "test2");
$passwords = array("password1", "password2");

$userIndex = array_search($username, $usernames, true);
$passIndex = array_search($password, $passwords, true);

if ($userIndex !== $passIndex || $userIndex === false) {
    die("authentication error");
}

However: having separate arrays of usernames and passwords, while it does work, is not really the most straightforward approach. It would be better if you have each username and its password "close together". If the decision is entirely in your hands you could for example use usernames as keys and passwords as values in the same array:

$auth = array("test1" => "password1", "test2" => "password2");

This way you can do it much more easily and without any need for business functions:

if (!isset($auth[$username]) || $auth[$username] !== $password) {
    die("authentication error");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Do you mean return $i in your search function? Also, $ar[$i]?

Though, honestly, it'd be smarter to use the standard library function for this, array_search(), which returns the index of an element of an array if it's in the array.

2 Comments

I have actually been through php.net/array and it does not mention array_search() until the bottom comments section and it does not explain what it is doing.
@Ali try the function reference for arrays. The particular page for array_search() is at php.net/manual/en/function.array-search.php.
0

If i had to prefer something apart from array_search, I would use foreach rather than for

foreach($array_name as $key => $value) {
    echo $key;     // echoes index of an array
    echo $value;   // echoes value of an array
}

Comments

-2

Use array_search() instead?

http://www.w3schools.com/php/func_array_search.asp

UPDATE:

$userNameIndex = array_search($userName, $usernames);
$paswordIndex = array_search($pasword, $passwords);

4 Comments

Please don't reference w3fools.com as a Stack Overflow answer. At least point to the relevant PHP manual entry.
@rdlowrey: Why not? Why is w3schools not an acceptable reference?
@Stefan Just follow the link in my comment to learn more. If you'd like, I can add a downvote then you can delete your answer to get the "Peer Pressure" badge :)
Monkey-mind learned-association train: 'References w3schools - don't have to think - just VOTE DOWN - and feel important.' Who cares if reference was accurate or suitable enough in this case or not. Let's not waste our important time on that, shall we. (Sarcasm intended). Ever heard of the term "throwing the baby out with the dishwater?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.