Stripslashes all values in an array with PHP.

Posted on Friday the 14th of August, 2009 at 1:38 pm in Dev

Here is a very simple function to run stripslashes on an array and all of its child arrays. The function below is for PHP 5 and takes a single argument which should consist of an array.

  1. function unstrip_array($array){
  2. foreach($array as &$val){
  3. if(is_array($val)){
  4. $val = unstrip_array($val);
  5. }else{
  6. $val = stripslashes($val);
  7. }
  8. }
  9. return $array;
  10. }
  11. Use the following to copy and paste the code.

It loops through all elements of the array and if an element is an array it calls itself on th the child array to an infinite depth. Of course this can cause memory problems if your arrays are dynamically created with an excessively large depth, but for most uses, it will function perfectly.

Related posts

  1. Content Management posted the following on March 12, 2010 at 3:52 pm.

    You would think PHP would have added a function to strip slashes from an array. PHP rocks, don’t get me wrong.

    Since there is no functionality we are left to build it on our own.

    This is a great example.

    Reply to Content Management

Leave a reply

:) :D :( :o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: ;) :!: :?: :idea: :arrow: :| :mrgreen: