0

I have a form that builds a JSON string to be saved inside a database table where the form elements names are array based to improve JSON creation.

The names are created dynamically like the following:

table[column][name][0]
table[column][name][1]
table[column][name][2]
table[column][name][3]

But I am actually running accross a situation.

I know that I can access this POST value as:

$_POST[table][column][name][0]
$_POST[table][column][name][1]
 //.. and so on

But the problem is that I receive the element name as a string

table][column][name][0]

How can I get

$_POST[table][column][name][0] 

from

table[column][name][0]
Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
  • You can trivially change the string you have to `table.column.name.0` and then the answers from [this question](http://stackoverflow.com/questions/8537148/string-with-array-structure-to-array/8537238#8537238) would work without modification. Not voting as duplicate yet, but do take a look. – Jon Jan 30 '14 at 14:14
  • @Jon Changing the element name is not allowed. It's legacy code that used to only create the JSON string, not update. – Gilberto Albino Jan 30 '14 at 14:26
  • 1
    Changing the element name is not allowed but it's not that hard to call `preg_replace` after you have received it, right? Or perhaps to adapt the code from the linked answer to work with your particular format? – Jon Jan 30 '14 at 14:28
  • @Joh I may be missing something here but I don`t have (quoted) $_POST["table][column][name][0]"]. I have $_POST['table'] = array( 'column' => array( 'name' => array( 0 => 'THE VALUE' ) ) ); – Gilberto Albino Jan 30 '14 at 14:38
  • Please read carefully the question I linked to and its answers. They are about *setting* values, but getting them out is the same really. – Jon Jan 30 '14 at 14:41
  • @Jon I guess I got it right now. Considering the linked answer from hakre I just treated my string from a variable $key = str_replace("]", "", $elementName) and set $target = $_POST. The last resulting variable for $rv has now de value for $elementName to be matched on corresponding POST variable. – Gilberto Albino Jan 30 '14 at 15:20

0 Answers0