Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:42:30+00:00 2026-05-31T22:42:30+00:00

How would I get a value from two arrays at a specific index? I

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:42:31+00:00Added an answer on May 31, 2026 at 10:42 pm

    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");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have JSF code like: <h:inputText id=from value=#{fromToMBean.fromName}/> I would like to get this
How would get find an average from an array? If I have the array:
So I have a function that merges two arrays replacing variables that are in
The objective is to get an array of MemberExpressions from two LambdaExpressions. The first
How would one get the contents of the 'value' attribute of a select tag,
With the XML below, I would like to know how to get the value
Following is how you would get the current solution directory from an add-in: _applicationObject
Would appreciate some assistance i have a txt file witht he following contents: 1234|dog|apartment|two
I just ran into something weird. I have two JSON arrays which holds different
I have a byte array that I am reading in from a NetworkStream. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.