如何将带有3个参数的数组导出到json文件?

时间:2021-03-22 00:59:18

I would like to export datas to a json file. It would be a simple registration page. I have the datas, but I can not export them, because I have 3 parameters:

我想将数据导出到json文件。这将是一个简单的注册页面。我有数据,但我无法导出它们,因为我有3个参数:

-Username -Email -Password

-Username -Email -Password

   <html>
    <body marginleft="auto" marginright="auto" marginwidth="500px" >
      <form method="post">
        Username:
        <input type="text" name="username" placeholder="Username">
        <br>
        Email:
        <input type="text" name="email" placeholder="Email">
        <br>
        Password:
        <input type="text" name="password" placeholder="Password">
        <br>

       <?php
        $allDatas = json_decode(file_get_contents('data.json'), true);
        $usernames = array();
        $passwords = array();
        $emails = array();

        foreach ($allDatas as $data) {
            array_push($usernames, $data[0]);
        }
        foreach ($allDatas as $data) {
            array_push($passwords, $data[1]);
        }
        foreach ($allDatas as $data) {
            array_push($emails, $data[2]);
        }

        if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['username'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $foundedUser = false;

            foreach ($usernames as $key => $value) {
                if ($value == $username) {
                    echo "Error: Username exsists;";
                }
            }
            foreach ($emails as $key => $value) {
                if ($value == $email) {
                    echo "Error: Email registered;";
                }
            }

            array_push($usernames, $username);
            array_push($passwords, $password);
            array_push($emails, $email);

            unset($allDatas);
            $allDatas = array();
            ????
            }
        }
        ?>
        <input type="submit" value="Registration">
        <br>
    </body>
    </html>

Thank you for the answers!!

谢谢你的答案!!

2 个解决方案

#1


1  

There's no need to create separate $usernames, $emails, and $passwords arrays. You can simply loop through $allData to search for existing entries that match the registration.

无需创建单独的$ usernames,$ email和$ passwords数组。您只需循环遍历$ allData即可搜索与注册匹配的现有条目。

Second, you need to set $founduser when you find a match, and check that before adding the new user.

其次,您需要在找到匹配项时设置$ founduser,并在添加新用户之前检查它。

Third, you should add the new user to $allData, then write that out to the file as JSON.

第三,您应该将新用户添加到$ allData,然后将其作为JSON写入文件。

  <?php
    if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['username'])) {
        $allDatas = json_decode(file_get_contents('data.json'), true);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $foundedUser = false;

        foreach ($allDatas as $value) {
            if ($value[0] == $username) {
                echo "Error: Username exsists;";
                $foundUser = true;
                break;
            } elseif ($value[2] == $email) {
                echo "Error: Email registered;";
                $foundUser = true;
                break;
            }
        }
        if (!$foundUser) {
            $allDatas[] = array($username, $email, $password);
            file_put_contents('data.json', json_encode($allDatas));
        }

        unset($allDatas);
        }
    }
    ?>

#2


0  

So you want to create a json string from $usernames, $passwords and $emails these arrays. You need to create a new array and insert these arrays into that new array, after that just use json_encode you will get a new json string.

所以你想从$ usernames,$ passwords和$ emails这些数组创建一个json字符串。您需要创建一个新数组并将这些数组插入到该新数组中,之后只需使用json_encode就可以得到一个新的json字符串。

Outside your if condition, you have to do these.

在你的if条件之外,你必须这样做。

You have these:

你有这些:

array_push($usernames, $username);
array_push($passwords, $password);
array_push($emails, $email);

Do this:

$newArr = array();
$newArr[] = $username;
$newArr[] = $passwords;
$newArr[] = $emails;

$newstring = json_encode($newArr);

Let me know you are helpful or not.

让我知道你是否乐于助人。

#1


1  

There's no need to create separate $usernames, $emails, and $passwords arrays. You can simply loop through $allData to search for existing entries that match the registration.

无需创建单独的$ usernames,$ email和$ passwords数组。您只需循环遍历$ allData即可搜索与注册匹配的现有条目。

Second, you need to set $founduser when you find a match, and check that before adding the new user.

其次,您需要在找到匹配项时设置$ founduser,并在添加新用户之前检查它。

Third, you should add the new user to $allData, then write that out to the file as JSON.

第三,您应该将新用户添加到$ allData,然后将其作为JSON写入文件。

  <?php
    if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['username'])) {
        $allDatas = json_decode(file_get_contents('data.json'), true);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $foundedUser = false;

        foreach ($allDatas as $value) {
            if ($value[0] == $username) {
                echo "Error: Username exsists;";
                $foundUser = true;
                break;
            } elseif ($value[2] == $email) {
                echo "Error: Email registered;";
                $foundUser = true;
                break;
            }
        }
        if (!$foundUser) {
            $allDatas[] = array($username, $email, $password);
            file_put_contents('data.json', json_encode($allDatas));
        }

        unset($allDatas);
        }
    }
    ?>

#2


0  

So you want to create a json string from $usernames, $passwords and $emails these arrays. You need to create a new array and insert these arrays into that new array, after that just use json_encode you will get a new json string.

所以你想从$ usernames,$ passwords和$ emails这些数组创建一个json字符串。您需要创建一个新数组并将这些数组插入到该新数组中,之后只需使用json_encode就可以得到一个新的json字符串。

Outside your if condition, you have to do these.

在你的if条件之外,你必须这样做。

You have these:

你有这些:

array_push($usernames, $username);
array_push($passwords, $password);
array_push($emails, $email);

Do this:

$newArr = array();
$newArr[] = $username;
$newArr[] = $passwords;
$newArr[] = $emails;

$newstring = json_encode($newArr);

Let me know you are helpful or not.

让我知道你是否乐于助人。