Using the recursive addslashes function to escape an array has an array, which may be multidimensional. some values contain 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 \\\"
)
Reply to discussion (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 '
';
FILTER can be used
$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 ))
Your code should be changed like this
$arr=array('ii"',array('one'=>'ec"','two'=>5));function t(&$arr){ foreach($arr as $i=>&$v){ if(is_array($v)){ t($v); }else{ $arr[$i] = addslashes($v); } }}t($arr);print_r($arr);
Change ?? You can.
function t(&$arr){foreach($arr as $key=>$v){if(is_string($v)){$arr[$key]=addslashes($v);}else{t($arr[$key]);}}}
I prefer chaos.
function t(&$arr){ foreach($arr as $key=>$v){ if(is_string($v)){ $arr[$key]=addslashes($v); }else{ t($arr[$key]); } }}$arr=array('ii"',array('one'=>'ec"','two'=>5));t($arr);
Invalid argument supplied for foreach ()
Change ?? You can.
function t(&$arr){foreach($arr as $key=>$v){if(is_string($v)){$arr[$key]=addslashes($v);}else{t($arr[$key]);}}}
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);
Thank you for your patience. the answers you provided are correct, but I did not point out my problem. After reading the code for one day, I finally found out that the cause of the code I wrote was: $ arr = array ('II "', array ('one' => 'EC "', 'two' => 5 )); after the parameters I provided enter the function body, the most 'two' => 5 recursion to foreach is not an array, so the warning that the parameter is invalid for foreach is reported.
$ Arr = array ('II "', array ('one' => 'EC"', 'two' => 5 ));
Function t (& $ arr ){
Foreach ($ arr as $ v ){
If (is_string ($ v )){
$ Arr [] = addslashes ($ v); // if $ arr [] is used here, the element can only be added in the original array.
} Else {
T ($ v); // Here $ v is printed as array ('one' => 'EC "', 'two' => 5) when it recursively goes to foreach, it is no longer the original array of $ arr. it is a new array completely unrelated to $ arr.
}
}
}
It's okay to write the following!
$ Arr = array ('II "', array ('one' => 'EC"', 'two' => 5 ));
Function t (& $ arr ){
Foreach ($ arr as $ k =>$ v ){
If (is_string ($ v )){
$ Arr [$ k] = addslashes ($ v );
} Else {
If (is_array ($ v) {// after judgment is added, you can disable the warning that the parameter is invalid for foreach.
T ($ arr [$ k]); // the array from the original array foreach ('one' => 'EC "', 'two' => 5)
}