How to filter the values of an array in drupal In drupal we have check_plain function to filter the unwanted codes from a text, I have an array which I am storing directly, in that case, I need to check each value before storing, is there a function to which we can pass the array and it should filter and reset the array.

Ex - I have a form where users can add as many names as they want using (multiple textfield) the name of the textfield is - guarantors[]

So I have an array where all the userinput is there, inorder to filter, I need to filter each value of array using foreach.

is there a function or way, so that I can directly pass the array to a function and it should return me an array with safe values, i.e each values should be parsed using check_plain function.

I think an expample may help you answer better, here is my example array.

Array
(
[0] =&gt; Array
    (
        [keyword] =&gt; lucidus
        [color] =&gt; #00ff00
    )

[1] =&gt; Array
    (
        [keyword] =&gt; commoveo
        [color] =&gt; #998899
    )

[2] =&gt; Array
    (
        [keyword] =&gt; dolus
        [color] =&gt; #ff0099
    )

)


NOTE :: keyword and color are user input values. And those needs to be filtered, it is and can be a multidimensional array.

Many Thanks
This is an old question, but there didn't seem to be a decent answer yet...

There is no out-of-the-box solution but it's easy to write a function that recursively walks the array and applies a filter function to the keys and values.

&lt;?php
function clean_array($array, $filter = 'check_plain') {
  $clean = array();
  foreach ($array as $key =&gt; $val) {
    if (is_array($val)) {
      $clean[$filter($key)] = clean_array($val, $filter);
    }
    else {
      $clean[$filter($key)] = $filter($val);
    }
  }
  return $clean;
}
?&gt;


This also gives you the flexibility to use filter_xss or check_markup instead.

Hope that helps.