Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:20:12+00:00 2026-06-07T07:20:12+00:00

Consider a PHP script which generates a form, which looks like a table and

  • 0

Consider a PHP script which generates a form, which looks like a table and has the same recurring rows output many times:

Image of the example form generated by the snippet.

print('<form method="POST" action="index.php">
    <table>
    <tr>
       <th>Row #</th>
       <th><tt>name</tt></th>
       <th><tt>value</tt></th>
    </tr>');

for ( $i = 1; $i <= 4; $i++ )
{
    print('<tr>
        <th>' .$i. '</th>
        <td><input type="text" name="name_' .$i. '" size="20"></td>
        <td><input type="text" name="value_' .$i. '" size="4"></td>
    </tr>');
}

print('</table>
      <input type="submit" name="is_submit" value="Submit">
</form>');

When you submit this form, the $_POST array will look like this:

array (
  'name_1',
  'value_1',
  'name_2',
  'value_2',
  'name_3',
  'value_3',
  'name_4',
  'value_4',
  'is_submit',
);

What I wish to do, it to organize this input so it will look something like this:

array (
  1 => 
  array (
    'name',
    'value',
  ),
  2 => 
  array (
    'name',
    'value',
  ),
  3 => 
  array (
    'name',
    'value',
  ),
  4 => 
  array (
    'name',
    'value',
  ),
);

It means, that every field ending in the same row ID number is grouped together with fellow fields (in the same row) in a two-level deep array.

I have been doodling around with options to solve this and came up with the following “solution” (the organized array (above) is actually a dump of the $output created by this approach):

if ( array_key_exists('is_submit', $_POST) ) // Form is submitted properly
{
    unset($_POST['is_submit']); // Drop the status key

    foreach ( array_keys($_POST) as $key )
    {
        $key_exploded = explode("_", $key); // [0] is key 'basename', [1] is the 'row ID'
        $output[ $key_exploded[1] ][ $key_exploded[0] ] = $_POST[$key];
    }
}

The big problem is that this method looks a bit obfuscated and hard-coded. I think and hope that the more-advanced people around here could guide me for a more dynamical (that unset row is itchy, that I need to call unset() on every non-table row) and better method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:20:14+00:00Added an answer on June 7, 2026 at 7:20 am

    Change these lines:

    <td><input type="text" name="name_' .$i. '" size="20"></td>
    <td><input type="text" name="value_' .$i. '" size="4"></td>
    

    to

    <td><input type="text" name="foobar['.$i.'][name]" size="20"></td>
    <td><input type="text" name="foobar['.$i.'][value]" size="4"></td>
    

    $_POST[‘foobar’] now will have the desired structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following php code used for HTML output: echo <div id='logo2'> <a href='if(checkLanguage())
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields
Consider a PHP script that outputs all POST data, as follows: <?php var_dump($_POST); ?>
I need to display continuous output from my php script. The script will stay
Consider, I've got this code in PHP <?php if($count==0) { ?> <script> show_my_div(); </script>
I am writing a php script which will run through cron . Its task
Consider these two table: <table id=prototype style=display:none;> <tr> <td></td> <td><input type=text ></td> <td><?php echo
I've written a PHP script that handles file downloads, determining which file is being
Consider this simple example; <?php $text = test; ?> <script type=text/javascript defer=defer> var test;
Consider a web app in which a call to the app consists of PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.