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
  • I knew variable variable were a bit hacky, but I am frankly astounded that PHP allows square brackets (and curly braces) in variable names, so long as they are created this way. Commented Jun 20, 2012 at 14:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think this is the way forward, changing my data structure to work with the built-in features rather than against them.
True, but I later noticed that the "more complex" structure in my note won't work... so if it's not the problem this is the way to go. Otherwise you might have to parse the keys yourself, and that might be hard/dangerous.
I toyed with using eval()... which would be easy to get working, but quite hard to get working securely, given that I am passing in user generated data. In the end I parsed some keys myself, but changed the data structure to keep it to a minimum.

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.