如何将数据作为数组的索引数组(不指定索引) [英] How to POST data as an indexed array of arrays (without specifying indexes)

查看:88
本文介绍了如何将数据作为数组的索引数组(不指定索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我知道如果我的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]>

我的问题是输入字段在表的行上,用户可以添加/删除多行如他所愿,所以我不能有固定的索引(或者每次添加行时我都必须修改输入字段的名称,因为每当我添加一行时,我都会克隆上一行所以我要求的是以下两件事之一:

1)有没有2)如果没有,我怎样才能动态修改新的输入字段,以便他们有一个更新的名称与我想要的方式发布数据的方式,而不是指定一个索引 新的索引?



编辑 - 我曾尝试使用 name =someName [value] [] name =someName [description] []但输出不是所需的:

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



<我知道我可以在PHP中迭代这个数组,我只是想知道我是否可以避免它。 解决方案

你提出这个问题的方式。如果用户删除某行,您的表单元素将为:

 < form action =...method = postonsubmit =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 循环。

 <?php 
if(count( $ _POST ['somename'])> 0)
{
foreach($ _POST ['somename'] as $ row)
{
echoValue:。$ row ['value']。< br /> \\\
;
echoDescription:。$ row ['description']。< br /> \\\
;




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

 <?php 
if(count($ _ POST ['somename'])> 0)
{
$ i = 0;
foreach($ _POST ['somename'] as $ row)
{
echoIndex $ i< br /> \\\
;
echo值:。$ row ['value']。< br /> \\\
;
echoDescription:。$ row ['description']。< br /> \\ \\ n;
$ i ++;
}
}

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



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

 < script type =text / javascript> 
函数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)
{
//在您处理完每行的所有
//输入后,只增加计数器(或行号)
counter ++;
}
}
}
< / script>


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:

<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

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

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

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>

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 />\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:

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆