Wrapping an entire form in array on PHP POST

I am trying to wrap an entire form in an array. Currently looks like:

<div class="round">
    <input name="points">
    <input name="player1[]">
    <input name="player2[]">
</div>

The entire form/div can be duplicated, so, ideally, I would have an output that could be spit out as

Read More
round[0][points]
round[0][player1[0,1,2]]
round[0][player2[0,1,2]]

It needs to be dynamic, so simply adding round[0] as static text before everything won’t work.

I have tried to wrap all of it in a , tried fieldset, but everything fails or spits out a terrible order of items.

Any ideas?

Edit to clarify desired output:
After all is said and done, I want this to be dynamic, hence the need for arrays within arrays. The output, after duplication, could be:

round[0][points]
round[0][player1[0,1,2]]
round[0][player2[0,1,2]]

round[1][points]
round[1][player1[0,1,2,3,4,5]]
round[1][player2[0,1,2,3,4,5]]

round[2][points]
round[2][player1[0,1]]
round[2][player2[0,1]]

Related posts

Leave a Reply

2 comments

  1. You’re over thinking it simply add brackets after points like this:

    <input name="points[]">
    <input name="player1[]">
    <input name="player2[]">
    

    When you go to process in it php just use a foreach loop:

    $player1 = $_POST['player1'];
    $player2 = $_POST['player2'];
    foreach($_POST['points'] as $i => $points){
       echo $points." ".$player1[$i]." ".$player2[$i];
    }
    

    Which should give you the results:

    points[0] player1[0] player2[0]
    points[1] player1[1] player2[1]
    etc.
    

    This should work (if I understand the question correctly).

    EDIT:

    Okay so I’m assuming the player1 & player2 input values are comma separated lists. If they are we just have to make an adjustment in the php:

    $player1 = $_POST['player1'];
    $player2 = $_POST['player2'];
    foreach($_POST['points'] as $i => $points){
       echo "Round ".$i.": "$points;
       echo "Team 1";
       $team1 = explode(",", $player1[$i]);
       foreach($team1 as $i => $player){
          echo "Player ".$i.": ".$player;
       }
       echo "Team 2";
       $team2 = explode(",", $player2[$i]);
       foreach($team2 as $i => $player){
          echo "Player ".$i.": ".$player;
       }
    }
    
  2. Partial answer: this gets the “player” array into the “round” array, now I just have to figure out how to auto-increment the round[] like I want to:

     <input name="_round[0][_player1][]">
    

    But I think I could just use a counter for that… despite how much I hate counters.