另一个数组的阵列组?

时间:2016-04-21 15:06:36

标签: php html arrays string

我正在尝试按另一个数组对一个数组进行排序。这两个数组都从表单中获取内容。

这是我的表单代码:

<form method="post" action="">

<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>

<br/>

<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>

<br/>
<input type="submit" name="submit" value="Submit" />
</form>

以下是PHP代码:

<?php

if (!$_POST['submit'] == "") {
    foreach($_POST['groupname'] as $groupname) {
        $groupnum = 1;
        foreach($_POST['variable'] as $variable) {  
            print "$".$groupname.$groupnum." = '".$variable."';<br/>";
            $groupnum++;
        }
        print "$".$groupname." = array(";
        for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
            print "$".$groupname.$arrnum.", ";
        }
        print ");<br/><br/>";
    }
}

?>

这是我提交表单时得到的结果:

$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )

$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )

这是我真正想要的结果:

$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)

$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)

整个事情需要是动态的,因为我想添加任意数量的组和变量。

我几天来一直在寻找答案,并且已经问过两个不知道答案的人。也许你们可以提供帮助。谢谢!

更新

只是澄清几点:

  1. 所以基本上我希望能够添加尽可能多的输入表单(我使用jQuery)来创建任意数量的组和变量,例如:

    $ groupwuteva1 ='你好'; $ groupwuteva2 ='再见':

    $ randomname1 ='green'; $ randomname2 ='blue'; $ randomname3 ='red';

    $ blabla1 ='abc'; $ blabla2 ='xyz'; $ blabla3 ='123'; $ blabla4 ='bla';

  2. 我用作组名的任何内容都将在数组1中使用,例如我将一组称为“颜色”,我在该组中添加的变量是“蓝色”,“红色”和“绿色”。然后我会得到这段代码:

    $ colors1 ='green'; $ colors2 ='blue'; $ colors3 ='red';

  3. 我希望这可以克服一些问题。到目前为止,感谢所有回复!

2 个答案:

答案 0 :(得分:1)

您可以将组名作为容器来存储其中的所有关联变量值。稍后,使用variable variablesimplode()函数处理您的html表单。

<强> HTML

<form method="post" action="">

<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>

<br/>

<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>

<br/>
<input type="submit" name="submit" value="Submit" />
</form>

<强> PHP

if(isset($_POST['submit'])){
    foreach($_POST['groupname'] as $value){
        $arr = array();
        $i = 1;
        foreach($_POST[$value] as $v){
            $var = $value . $i;
            $$var = $v;
            echo $var . " = " . $$var . "<br />";
            $arr[] = $$var;
            ++$i;
        }

        $output = $value . " = array(" . implode(",", $arr) . ")";
        echo $output . "<br /><br />";
    }
}

<强>输出:

groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)

grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)

答案 1 :(得分:0)

尝试以下形式:

//div[contains(span, 'TextA')]/following-sibling::div/span

和代码:

<form method="post" action="">
    <?php foreach(array('groupone', 'grouptwo') as $num):?>
        <div class="groupcontainer">
            <br/><label>Groupe <?php echo $num;?>:</label><br/>
            <input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
            <br/><label>Variable Group <?php echo $num;?>:</label><br/>
            <input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
            <input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
        </div>

        <br/>
    <?php endforeach;?>

    <input type="submit" name="submit" value="Submit" />
</form>