Sarathlal N

Sanitize array using WordPress sanitization functions

Here is the array I need to sanitize.

$array = array(
    'key_1' => 'array elemnt 1',
    'key_2' => 'array element 2'

Now I need to sanitize array key & value using same sanitization function. If so, we can use array_map function.

$new_array = array_map('sanitize_key', $array);

But some times, I need to sanitize array key & values using different sanitization functions.

$array = array(
    'key_1' => 'array elemnt 1',
    'key_2' => 'array element 2'

$keys = array_keys($array);
$keys = array_map('sanitize_key', $keys);

$values = array_values($array);
$values = array_map('sanitize_text_field', $values);

$array = array_combine($keys, $values);

Recent Posts

  1. Solve "ValueError invalid literal for int() with base 10" - Python
  2. Handling dynamic subdomain with Flask - Python
  3. A minimal example about WordPress object cache
  4. Select random element from a list - Python
  5. Write our first Selenium program with Python 3 & Firefox

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.