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
2010-08-06 08:13:06
This gives warning: stripslashes() expects parameter 1 to be string,
Starx
2010-08-06 09:58:04
@jdoef: So your array does not only contain strings?
Gumbo
2010-08-06 10:14:45
@Gumbo, not neccesarily. can't the script check if it is string and then only strip the slashes
Starx
2010-08-06 10:24:46
@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
2010-08-06 11:18:19
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
2010-08-06 08:23:35
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
2010-08-06 08:31:18