I might just be tired or something, but I can't figure out how I can, based on an <input>
's name attribute access the correct $_POST
variable.
With simple inputs it's easy;
<input name="email">
$inputName = 'email';
$inputValue = $_POST[$inputName];
But how does it work with arrays?
<input name="foo[bar][0]">
$inputName = 'foo[bar][0]';
$inputValue = $_POST[$inputName]; # Obviously fails...
So how can I convert this to $_POST[foo][bar][0]
, is there some built in PHP function for this?
I'm not sure how this is a duplicate of that other question seeing as he wants to access an array using dot.separation
and I want to use the standard form name way?
A little more background; This is for a Form handling PHP class I'm building, where the user can add fields like this:
$form->addField(['name' => 'my[field][name]']);
And later when I want to fetch the value of the submitted field all I have to go by is the name specified by the user.
If I was manually doing this I would obviously just write $_POST[foo][bar][0]
but now I can never know the name of the field and thus I have to parse it somehow.
Ok so this has expanded a bit into me having to know, based on an input name, what that input's value is, or if that input is selected (for select, radio elements) or if that input is part of an array of values (multiple select, checkboxes). I'd also like it to support any input name, such as foo[bar][][][baz][12][]
.
If anyone has done something similar I'd love to hear about it, but it's leaning towards me writing custom code for this myself.