【问题标题】:PHP Create Multidimensional Array From Form DataPHP 从表单数据创建多维数组
【发布时间】:2018-08-24 07:02:03
【问题描述】:

昨天我整天都在寻找解决方案,但我仍然卡住了

这是我的问题:

我有一个 PHP 页面,我必须在其中填写一个包含 3 个值的表单, 当我点击提交时,它应该在 JSON 数组中创建一个新的 JSON 数组,数组的编号增加 1。

我想要达到的目标:

当我填写表单 5 次并单击提交按钮 5 次时,我的 JSON 数组应该如下所示:

{
  "articles": {
    "1": {
      "tax": "11",
      "price": "111",
      "discription": "Transport"
    },
    "2": {
      "tax": "234",
      "price": "4532",
      "discription": "Opslag"
    },
    "3": {
      "tax": "19",
      "price": "19",
      "discription": "Gasoline"
    },
    "4": {
      "tax": "84765",
      "price": "4235",
      "discription": "Food"
    },
    "5": {
      "tax": "132",
      "price": "5460",
      "discription": "Opslag"
    }
  }
}

我想做什么:

我尝试在 javascript 中创建一些东西:它从所有输入字段创建一个 JSON 数组,但到目前为止我无法存储这些字段并从结果中创建一个多维数组。我确实发现了一个有趣的堆栈溢出问题Click,但我不知道如何使用输入字段来填充数组,就像问题上的示例一样。

我希望有人可以帮助我:)

托马斯

【问题讨论】:

  • 您尝试过任何代码吗?因为很难猜出您到底想达到什么目标。
  • 你需要这个服务器端还是客户端? 我尝试用 javascript 创建一些东西,但没有成功。好的,你尝试了什么?请发布MCVE,以便我们真正帮助解决您遇到的问题。
  • @Loek 我需要这个服务器端
  • 我们需要查看您的代码
  • @Joseph_J 哪个代码?

标签: php arrays json multidimensional-array


【解决方案1】:

您可以通过两种方式做到这一点。两者都合适。我想我会认为第二种方式更安全,因为它完全是在服务器端完成的。我会为你演示。

第一种方式:

您将使用隐藏的输入字段并序列化一个数组。您将通过隐藏的输入字段在提交时将序列化数组传递回您的 post 数组。代码会将新的帖子数据推送到它从隐藏输入字段中获得的未序列化数组中。

像这样:

<?php

if(isset($_POST['submit']) && $_POST['submit']){

    $array = unserialize(base64_decode($_POST['articles']));

    $array['articles'][] = array(

        'tax'               => $_POST['tax'],
        'price'             => $_POST['price'],
        'description'       => $_POST['description']

    );

    $postData = base64_encode(serialize($array));

}

?>

<!DOCTYPE HTML>
<html>

  <head>
    <title>My Simple Form</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
  </head>

  <body style="background-color:#b3ffff">

        <div style="padding-left:500px; padding-top:200px">

            <form action=""  method="post" enctype="multipart/form-data">

                Tax: <input type="text" name="tax" placeholder="tax" value=""><br>
                Price: <input type="text" name="price" placeholder="price" value=""><br>
                Description <input type="text" name="description" placeholder="description" value=""><br>
                <input type="hidden" name="articles" value=" <?php echo $postData; ?> ">
                <input type="submit" name="submit" value="Submit">

            </form>

    </div>

  </body>

</html>


<?php

echo
'<pre>';
print_r($array);
echo
'</pre>';

 //When you need to convert it to a json string then use this:
 $jsonString = json_encode($array);

?>

第二种方式

这种方式不使用隐藏的输入字段。相反,我们只是将发布数据传递给 $_SESSION 变量,该变量将数组存储在服务器端的内存中。只要确保在您决定离开页面时删除会话变量,因为如果您不这样做,它将始终存在。我的意思是,如果您稍后重新加载该页面,那么您第一次访问该页面时的所有数据仍然存在。

session_start();

if(isset($_POST['submit']) && $_POST['submit']){

    $_SESSION['myData']['articles'][] = array(

        'tax'               => $_POST['tax'],
        'price'             => $_POST['price'],
        'description'       => $_POST['description']

    );

}

?>

<!DOCTYPE HTML>
<html>

  <head>
    <title>My Simple Form</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
  </head>

  <body style="background-color:#b3ffff">

        <div style="padding-left:500px; padding-top:200px">

            <form action=""  method="post" enctype="multipart/form-data">

                Tax: <input type="text" name="tax" placeholder="tax" value=""><br>
                Price: <input type="text" name="price" placeholder="price" value=""><br>
                Description <input type="text" name="description" placeholder="description" value=""><br>
                <input type="submit" name="submit" value="Submit">

            </form>

    </div>

  </body>

</html>


<?php

echo
'<pre>';
print_r($_SESSION['myData']);
echo
'</pre>';

【讨论】:

  • 刚刚完成了一个新表和其他东西的替代方案,这也适用于我我肯定会使用你的示例,非常感谢:)
  • 酷,我希望你项目的其余部分一切顺利! ~干杯
【解决方案2】:
$array[] = $_POST["value"]; // push array after submit

$strJSON = json_encode($array);    //convert Array to JSONString
$objJSON = json_decode($strJSON);  //convert JSONString to JSONObject

【讨论】:

  • 您能进一步解释为什么这会有所帮助吗?对我来说,这看起来很奇怪......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 2014-09-25
  • 2023-03-16
相关资源
最近更新 更多