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

How to POST data as an indexed array of arrays (without specifying indexes)(如何将数据作为数组的索引数组发布(不指定索引))
本文介绍了如何将数据作为数组的索引数组发布(不指定索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)
) 

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

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

<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) 有没有一种方法可以在不指定索引的情况下以我想要的方式发布数据

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

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

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

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

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

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

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

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

推荐答案

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

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>

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

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

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

如果你需要知道每一行的编号作为连续索引(在提供的例子中,第0行仍然是0,但第2行应该是1(因为用户删除了一行),你可以使用一个变量充当计数器:

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:

<?php
if (count($_POST['somename']) > 0)
{
    $i = 0;
    foreach ($_POST['somename'] as $row)
    {
        echo "Index $i<br />
";
        echo "Value: ".$row['value']."<br />
";
        echo "Description: ".$row['description']."<br />
";
        $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:我修改了第一段代码以包含

元素.这将是伴随的 js 函数:

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

<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>

这篇关于如何将数据作为数组的索引数组发布(不指定索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)