tags:

views:

29

answers:

4

I need to stripslashes all items of an array at once. Do you how to............

+4  A: 
foreach ($your_array as $key=>$value) {
$your_array[$key] = stripslashes($value);
}

or for many levels array use this :

function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}


$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);

print_r($array);
Gatman
+1  A: 

You can use array_map:

$output = array_map('stripslashes', $array);
Gumbo
This gives warning: stripslashes() expects parameter 1 to be string,
Starx
@jdoef: So your array does not only contain strings?
Gumbo
@Gumbo, not neccesarily. can't the script check if it is string and then only strip the slashes
Starx
@jdoef: Well, you could write your own function like `function foo(array $arr) { $ret = array(); foreach ($arr as $key => $val) { if (is_string($val)) $val = stripslashes($val); $ret[$key] = $val; } return $ret; }`
Gumbo
A: 

Parse array recursevely, with this solution you don't have to dublicate your array

function addslashes_extended(&$arr_r){
    if(is_array($arr_r))
    {
        foreach ($arr_r as &$val){
            is_array($val) ? addslashes_extended($val):$val=addslashes($val);
    }
        unset($val);
    }
    else
        $arr_r=addslashes($arr_r);
return $arr_r;
}
Centurion
A: 

For uni-dimensional arrays, array_map will do:

$a = array_map('stripslashes', $a);

For multi-dimensional arrays you can do something like:

$a = json_decode(stripslashes(json_encode($a)), true);

This last one can be used to fix magic_quotes, see this comment.

Alix Axel