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);
Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.
Recent Posts
- Disabling Payment Methods in WooCommerce Based on Conditions
- How to Update Product Quantity in WooCommerce Using Custom Code
- Dynamically Generating a Table of Contents in WordPress
- Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
- Understanding the Impact of git reset --hard Command
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.