简体   繁体   English

在 php 中向多维数组中插入新元素

[英]Inserting new element to multi-dimensional array in php

<?php
  $accounts = [["felh" => "minta", "password" => "Meow123", "mail" => "pelda@gmail.com"],
  ["felh" => "minta2", "password" => "Meow1232", "mail" => "pelda2@gmail.com"]];
 if (isset($_POST["regisztral"])) {
    $username = $_POST["felh"];
    $password = $_POST["password"];
    $email = $_POST["mail"];
    $password2 = $_POST["password2"];
       if (strlen($password) < 8)
       die("<b>error<b>");
       if ($password !== $password2)
       die("<b>error</b>");
        if ($_POST['feltetel'] != 'igen') die("<b>error</b>");
        foreach ($accounts as $account) {
        if ($account["felh"] === $username)     die("<b>error:</b> taken!");
         if ($account["mail"] === $email)     die("<b>error:</b>taken!");
        }

            $accounts[] = ["felh" => $username, "password" => $password, "mail" => $email];
        $file = fopen("accounts.txt", "w");
             foreach ($accounts as $account)
             fwrite($file, serialize($account) . "\n");
             fclose($file);

     echo "<br>Succeed<br/>";
     }

The user has to fill in a registration form and my program should decide whether his username and email adress are taken or not.用户必须填写注册表,我的程序应该决定是否使用他的用户名和 email 地址。 I want to sort all usernames, passwords and email adresses into a 2 dimensional array and then write it into a text file.我想将所有用户名、密码和 email 地址排序为二维数组,然后将其写入文本文件。 My problem is that every time I insert a new record into the array, it switches the last element to the one I just inserted instead of keeping it and adding the new element to the end of the array.我的问题是,每次我在数组中插入一条新记录时,它都会将最后一个元素切换到我刚刚插入的那个,而不是保留它并将新元素添加到数组的末尾。 How could I solve it?我该如何解决?

*edit I uploaded the entire php section of my code. *编辑我上传了我的代码的整个 php 部分。 So to be clear, my problem is the following: the 2 records I added manually at the beginning of the code work perfectly.需要明确的是,我的问题如下:我在代码开头手动添加的 2 条记录完美运行。 When I want to insert let's say cat aaaaaaaa and nothing@gmail.com it Works perfectly.当我想插入让我们说 cat aaaaaaaa 和 nothing@gmail.com 它工作得很好。 But when I submit another value after that, let's say dog bbbbbbbb and something@gmail.com it switches cat aaaaaaaaa and nothing@gmail.com to dog bbbbbbbb and something@gmail.com.但是,当我在那之后提交另一个值时,假设 dog bbbbbbbb 和 something@gmail.com 它将 cat aaaaaaaaa 和 nothing@gmail.com 切换到 dog bbbbbbbb 和 something@gmail.Z4D236D1502D102C50ZFE6。 What I want however is keeping cat aaaaaaaa nothing@gmail.com and THEN adding dog bbbbbbbb something@gmail.com to the end of the array然而,我想要的是保持 cat aaaaaaaa nothing@gmail.com 然后将 dog bbbbbbbb something@gmail.com 添加到数组的末尾

What is causing problem for you is php function fopen() and mode.对您造成问题的是 php function fopen() 和模式。 Currently in code you use mode 'w' and by php manual that mode is:目前在您使用模式“w”的代码中,通过 php 手册,该模式为:

Open for writing only;只供写作; place the file pointer at the beginning of the file and truncate the file to zero length.将文件指针放在文件的开头并将文件截断为零长度。 If the file does not exist, attempt to create it.如果该文件不存在,请尝试创建它。

Everytime you overwrite this file named accounts.txt with new data.每次用新数据覆盖这个名为 accounts.txt 的文件时。

So you should use mode 'a' to place pointer at the end of file and append new data.所以你应该使用模式'a'将指针放在文件末尾和 append 新数据。

Open for writing only;只供写作; place the file pointer at the end of the file.将文件指针放在文件末尾。 If the file does not exist, attempt to create it.如果该文件不存在,请尝试创建它。 In this mode, fseek() has no effect, writes are always appended.在这种模式下, fseek() 没有效果,写入总是附加的。

test: https://repl.it/@kallefrombosnia/DiscreteOnerlookedLists测试: https://repl.it/@kallefrombosnia/DiscreteOnerlookedLists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM