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 8117813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:17:53+00:00 2026-06-06T04:17:53+00:00

I am trying to process the submitted results for a form, containing data for

  • 0

I am trying to process the submitted results for a form, containing data for a number of employees. The form inputs have names like “employees[1]_firstName” which needs to map to the PHP variable $companydata->employees[1]->firstName

When populating the $_POST array, PHP sees square brackets, and tries to make a multi-dimensional array, but gets it wrong (ignoring everything after the opening bracket)

This replicates $_POST but without the corrupted array keys: Note that I’ve taken out a foreach loop to simplify the question.

$post_data = explode('&', file_get_contents("php://input"));
// Result: $post_data = array('employees%5B1%5D_firstName=Timothy'

list($key, $value) = explode('=', $post_data[0]);
$key = urldecode($key);
$value = urldecode($value);
// Result: $key = 'employees[1]_firstName',  $value = 'Timothy'

However things go wrong when I try to use variable variables:

$post_key_parts = explode('_', $key);
// Result: $post_key_parts = array([0] => 'employees[1]', [1] => 'firstName')

$Companydata->$post_key_parts[0]->$post_key_parts[1] = $value;
// Expected result: Element [0] in array $employees => 'Timothy'

The actual result is a variable with square brackets in its name ‘$employees[0]’,
and no change to the $employees array. Putting curly brackets round the {$post_key_parts[0]} doesn’t help.

I am trying to find a flexible solution that will also work for names of different lengths eg: employees[0]_address_lines[2] or employees[0]_addresses[1]_postcode

I’m happy to avoid the sin of variable variables, but I can’t think of an elegant way to do it with regexes or something like that.

  • 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-06T04:17:54+00:00Added an answer on June 6, 2026 at 4:17 am

    I would suggest you change the inputs’ names. To make use of the built-in features of PHP. [] creates an array for you, use arrays then, as you are intended to use them.

    I had the same problem and came up with this:

    //HTML part
    <input name="employee_firstName[]">
    <input name="employee_address[]">
    
    //PHP part
    <?php
    $info = array("firstName", "address")
    foreach ($info as $i) {
        foreach ($_POST["employee_".$i] as $k => $v) {
            $companydata->employees[$k]->$i = $v;
        }
    }
    ?>
    

    NOTE I don’t think you can do this for things like employees[0]_address_lines[2]. Or maybe try employees[0][address_line][] and use it as an array, but I’m not sure that works.

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

Sidebar

Related Questions

I have a form which is submitted to a PHP page. The data from
I'm new to Django, trying to process some forms. I have this form for
I'm trying to process data obtained from a run of diff to an instance
I am trying to process a CSV file in which some of the fields
I am trying to process results to change how the dates are displayed on
I'm trying to stay on the current page from where a form gets submitted.
I'm trying to upload a file using AJAX, process data in the file and
while trying to process some user input which contains characters such as <. I
I am trying to process a csv file using awk. I have five rows
I'm trying to make my contact form submission process work equally well whether the

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.