更新PHP数组,并更新服务器端

时间:2014-01-13 14:05:42

标签: php html arrays

我正在搞乱一些php并且不熟悉如何更新阵列正常工作。这就是我现在所拥有的:

的index.php

<form action="register.php" method="get">
register > 
    <input name="reg_username" type="text" />
    <input name="reg_password" type="password" />
    <input id="submit" type="submit" />
</form>

register.php

    <?php
    $username = $_GET["reg_username"];
    $password = $_GET["reg_password"];
        include('data/user_data.php');
        if(isset($users[$username])):
            echo "this username is already taken!";
        else: 
            array_push($users, $username, $username, $password);            
        endif;  
?>

user_data.php

<?php $users = array(
dextermb => array("dextermb", "password"),
tonymb => array("tonymb", "password2")
)
?>

我想确保在更新阵列时它保持与其中已存在的格式相同的格式。

"username" => array("username","password")

3 个答案:

答案 0 :(得分:1)

只需使用用户名作为密钥和包含用户名和密码的数组向数组添加新元素:

if(isset($users[$username])):
    echo "this username is already taken!";
else: 
    $users[$username] = array($username, $password);
endif;  

答案 1 :(得分:0)

你有没有试过这个?

$users[$username] = array($username, $password );

为了简化使用,您也可以尝试以下代码

$users[$username] = $password; // If you have no other data in array

答案 2 :(得分:0)

array_push()仅适用于非关联数组。您可以使用以下命令显式设置数组键及其关联值:

$array[$key] = array();

因此,您的代码应如下所示:

if(isset($users[$username])):
    echo "this username is already taken!";
else: 
    $users[$username] = array($username, $password);