如何從特定索引處的兩個數組中獲取值?我有一個$usernames
陣列和一個$passwords
陣列。我也有變量叫做$username
和$password
(這是用戶輸入的用戶名和密碼)。數組在索引處的值(PHP)
我想要得到的$username
的指數$usernames
和$passwords
比較它的$password
索引。如果它們匹配,則用戶名和密碼是正確的。如果不是,他們是不正確的。
我知道數組可能不是最好的辦法,但它只適用於5人,並不一定是絕密,只是一個簡單的密碼保護。
我曾嘗試:
<?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;
}
}
?>
但是它返回
它說:
和密碼:我
**答案:**閱讀手冊。 – rdlowrey 2012-03-24 17:56:32
http:// php。net/array – hakre 2012-03-24 17:57:11
保存自己很多工作,並使用關聯數組來代替:'$ users = array(「user1」=>「password1」,「user2」=>「password2」);'然後檢查if' $ users [$ username] == $ password){...'。 – 2012-03-24 18:11:12