Skip to content
Advertisement

PHP: How to escape HTML form input multidimensional keys

I have form that have input fields with multidimensional keys. And keys contains XPath queries.

When i submit my form, keys of array spoiling.

This is simple example of my code.

JavaScript

Response of var_dump

JavaScript

How can i escape keys. I hope someone can give an answer.

Please do not offer to resolve this issue with json or with another way.

Advertisement

Answer

You can use urlencode to escape the $attr value, and then use array_walk over $_POST['key'] to replace the keys with their urldecode version:

JavaScript

Output:

JavaScript

If you have multiple elements under each numeric key value, you will need to use a foreach within the array_walk:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement