3

I'm trying to sort one array by another array. Both these arrays get their content from a form.

Here's my form code:

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

Here's the PHP code:

<?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/>";
    }
}

?>

This is the result I get when I submit the form:

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

This is the result that I actually want:

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

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

The whole thing needs to be dynamic since I want to add as many groups and variables as I want.

I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!

Update:

Just to clarify a few points:

  1. So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:

    $groupwuteva1 = 'hello'; $groupwuteva2 = 'bye':

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

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

  2. Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:

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

I hope this clairfies some questions. And thanks a ton for all responses so far!

2
  • I don't think you are going to achieve what you want with that approach without making major changes. It looks like you are presuming that the PHP POST array will follow the nested structure of your HTML but this will not be the case. The POST array will have 'group names' in one array and 'variables' in a separate array with no indication of how they are related. Commented Apr 21, 2016 at 15:25
  • Thanks to all you guys for helping. However, all the solutions posted look like they require me giving the inputs fixed names like name="groupone" that needs to fit the input of value="..." of the first array. But giving them fixed names misses the point of typing a name into the form... Maybe I'm just not getting it :P Commented Apr 21, 2016 at 20:30

2 Answers 2

1

You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.

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 />";
    }
}

Output:

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

grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
Sign up to request clarification or add additional context in comments.

6 Comments

Hello Rajdeep, thank you for your answer. It definitely helps. The only problem is this only works if I call the group groupone and grouptwo, etc. If I want to call them something else I get this error: Warning: Invalid argument supplied for foreach(). If for example I put the groupname colors into the form then the name of the variables needs to be name="colors[]" right? Is there a way to do this? Thanks!
@db225 hmm, this should work. Please post your HTML code on pastebin.com and share the link here.
@db225 Your code works fine. What's the issue you're facing now?
Hey, thanks for checking the code. I've changed the group names and variable names now, maybe now you know what I mean. pastebin.com/M6YYXqd6
@db225 You've named all the groups arbitrarily and all the variables as groupone[]. With this way you can never group the group name and the associated variables. In my opinion this method is much more robust and is well suited for your situation.
|
0

try this form:

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

and code:

if ($_POST) {
    foreach($_POST['groupname'] as $groupname) {
        $$groupname = array();

        foreach($_POST['variable'][$groupname] as $variable) {
            ${$groupname}[] = $variable;
        }
    }

    var_dump($groupone);
    var_dump($grouptwo);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.