Using the recursive addslashes function to escape an array has an array, which may be multidimensional. some values contain special characters & nbsp; for example, "& nbsp; I add addslashes () recursively () escape & nbsp; failed to reach the effect & nbsp; help me to see what is wrong $ arrarray (ii, array (one & gt; ec, two & gt; 5 )); function & escape array with recursive addslashes function
There is an array, which may be multidimensional. some values in it carry special symbols such as "I use recursion to add addslashes () escape
Failed to achieve the effect. help me see what's wrong.
$ Arr = array ('II "', array ('one' => 'EC"', 'two' => 5 ));
Function t (& $ arr ){
Foreach ($ arr as $ v ){
If (is_string ($ v )){
$ Arr [] = addslashes ($ v );
} Else {
T ($ v );
}
}
}
T ($ arr );
Echo"
";
print_r($arr);
echo '
';
The front-end page is output like this
________________________________________________________
Warning: Invalid argument supplied for foreach () in D: \ AppServ \ www \ index. php on line 22
Array
(
[0] => ii"
[1] => Array
(
[One] => ec"
[Two] => 5
)
[2] => ii \"
[3] => ii \\\"
)
------ Solution --------------------
By yourself
$arr=array('ii"',array('one'=>'ec"','two'=>5),'aa"a');
function t(& $arr){
foreach($arr as $k => $v){
if(is_string($v)){
$arr[$k] = addslashes($v);
}else{
$arr[$k] = t($v);
}
}
return $arr;
}
t($arr);
echo "";
print_r($arr);
echo '
';
------ Solution --------------------
$arr = array('ii"',array('one'=>"ec'",'two'=>5));
array_walk_recursive($arr, function(&$v) { $v = addslashes($v);} );
print_r($arr);
Array
(
[0] => ii\"
[1] => Array
(
[one] => ec\'
[two] => 5
)
)
------ Solution --------------------
You can change it to this.
function t(&$arr){
foreach($arr as $key=>$v){
if(is_string($v)){
$arr[$key]=addslashes($v);
}else{
t($arr[$key]);
}
}
}
------ Solution --------------------
Change it.
function t(&$arr){
foreach($arr as $key=>$v){
if(is_array($v)){
t($arr[$key]);
}else{
$arr[$key]=addslashes($v);
}
}
}
$arr=array('ii"',array('one'=>'ec"','two'=>'5'));
t($arr);
print_r($arr);