Use recursive addslashes function to escape the array solution

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.