php 如何将数据作为索引数组发布(不指定索引)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6072383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:16:30  来源:igfitidea点击:

How to POST data as an indexed array of arrays (without specifying indexes)

phpjqueryhtmlpost

提问by Nicola Peluchetti

i'm having some problem with posting data as an array of array. This is how i'd like my data to be POSTED:

我在将数据作为数组数组发布时遇到了一些问题。这就是我希望我的数据被发布的方式:

array(
['someName'] =>
array([0] =>
      array(['description'] =>890
            ['valore'] =>444)
      [1] =>
      array(['description'] =>98090
            ['value'] =>77)
) 

I know i can achieve this if my html is like this:

我知道如果我的 html 是这样的,我可以做到这一点:

<input type='text' name="someName[0][value]">
<input type='text' name="someName[0][description]">
<input type='text' name="someName[1][value]">
<input type='text' name="someName[1][description]">

My problem is that the input fields are on rows of a table and the user can add/remove as many rows as he want, so i can't have fixed index (or i have to modify the name of the input fields each time a row is added since every time i add a row i clone the upper row in the table)

我的问题是输入字段位于表的行上,用户可以根据需要添加/删除任意数量的行,所以我不能有固定索引(或者我每次都必须修改输入字段的名称添加行是因为每次添加行时我都会克隆表中的上一行)

So what i am asking is one of these two things:

所以我要问的是这两件事之一:

1) is there a way to post data the way i want without specifing an index

1)有没有办法在不指定索引的情况下以我想要的方式发布数据

2)if not, how can i modify dynamically the new input field so that they have an updated name with the new index?

2)如果没有,我如何动态修改新的输入字段,以便它们具有带有新索引的更新名称?

EDIT - i had alredy tried using name="someName[value][]"and name="someName[description][]"but the output is not the desired one:

编辑 - 我已经尝试使用name="someName[value][]"name="someName[description][]"但输出不是想要的:

array(['terreniOneri'] =>
       array(['descrizione'] =>array([0] =>890
                                      [1] => 98090)
               ['valore'] =>array([0] =>444
                                  [1] =>677)
      ) 

i know i can iterate on this array in php i was just wondering if i could avoid it.

我知道我可以在 php 中迭代这个数组我只是想知道我是否可以避免它。

采纳答案by Carlos Campderrós

Do it the way you put in the question. If the user removes some row, your form elements would be:

按照你提出问题的方式去做。如果用户删除某行,您的表单元素将是:

<form action="..." method="post" onsubmit="return reindexer(this);">
    <input type='text' name="someName[0][value]">
    <input type='text' name="someName[0][description]">
    <input type='text' name="someName[2][value]">
    <input type='text' name="someName[2][description]">
</form>

But there's no problem to traverse an array with non-contiguous numeric indexes in php: use a foreachloop.

但是在 php 中遍历具有非连续数字索引的数组是没有问题的:使用foreach循环。

<?php
if (count($_POST['somename']) > 0)
{
    foreach ($_POST['somename'] as $row)
    {
        echo "Value: ".$row['value']."<br />\n";
        echo "Description: ".$row['description']."<br />\n";
    }
}

If you need to know the number of each row as a continous index (in the example provided, row 0 would still be 0, but row 2 should be 1 (as the user deleted one row), you can use a variable acting as a counter:

如果您需要知道作为连续索引的每一行的编号(在提供的示例中,第 0 行仍为 0,但第 2 行应为 1(因为用户删除了一行),您可以使用一个变量作为柜台:

<?php
if (count($_POST['somename']) > 0)
{
    $i = 0;
    foreach ($_POST['somename'] as $row)
    {
        echo "Index $i<br />\n";
        echo "Value: ".$row['value']."<br />\n";
        echo "Description: ".$row['description']."<br />\n";
        $i++;
    }
}

I think this approach has more sense that the other solutions, as this way you would have an array of items, being each item a value and a description, instead of having two separate arrays of values and descriptions and having to get the values for your item from those two arrays instead of one.

我认为这种方法比其他解决方案更有意义,因为这样你会有一个项目数组,每个项目都是一个值和一个描述,而不是有两个单独的值和描述数组,并且必须为你的这两个数组中的项目而不是一个。

edit: I've modified the first piece of code to include the <form>element. This would be the accompanying js function:

编辑:我修改了第一段代码以包含该<form>元素。这将是伴随的 js 函数:

<script type="text/javascript">
function reindexer(frm)
{
    var counter = 0;
    var inputsPerRow = 2;
    for (var idx = 0; idx < frm.elements.length; idx++)
    {
        elm.name = elm.name.replace('%%INDEX%%', counter);
        if (idx % inputsPerRow == 1)
        {
            // only increment the counter (or row number) after you've processed all the
            // inputs from each row
            counter++;
        }
    }
}
</script>

回答by Damien

Try like this:

像这样尝试:

<input type='text' name="someNameValue[]">
<input type='text' name="someNameDescription[]">

If the fields are paired, they can be attached by the indexes. So if you have the 10th row, someNameValue[9] and someNameDescription[9] will be a pair. You can merge them.

如果字段是配对的,它们可以由索引附加。所以如果你有第 10 行, someNameValue[9] 和 someNameDescription[9] 将是一对。你可以合并它们。

EDIT: You don't have to write the indexes manually, they will be automatically generated.

编辑:您不必手动编写索引,它们将自动生成。

<input type='text' name="someName[]">
<input type='text' name="someName[]">
<input type='text' name="someName[]">

and

<input type='text' name="someName[0]">
<input type='text' name="someName[1]">
<input type='text' name="someName[2]">

will give the same result in your post array.

将在您的帖子数组中给出相同的结果。