Escaping an array with a recursive addslashes function

Source: Internet
Author: User
There is an array, possibly multidimensional, with some values with special symbols such as "I escaped with recursion plus addslashes ()

It's not working, help, see what's wrong.
$arr =array (' II ', array (' one ' = ' "EC" ', ' =>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 '
';



This is how the foreground page is exported.
________________________________________________________

Warning:invalid argument supplied for foreach () in D:\AppServ\www\index.php on line 22
Array
(
[0] = = II "
[1] = = Array
(
[One] = EC "
[5] + =
)

[2] = ii\ "
[3] = ii\\\ "
)


Reply to discussion (solution)

You compare yourself.

$arr =array (' II ', array (' one ' = ' EC ', ' =>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 "
";p Rint_r ($arr); Echo '
';

You can use the FILTER

$arr = Array (' II ', array (' one ' = ' + ' EC ' ", ' =>5 ') array_walk_recursive ($arr, function (& $v) {$v = Addslashes ($v);} );p Rint_r ($arr);
Array (    [0] = ii\ "    [1] = = Array        (            [One] = Ec\ ' [One] =            5        ))

Your code is going to change that.

$arr =array (' II ', array (' one ' = ' EC ', ' =>5 ')), function T (& $arr) {  foreach ($arr as $i =>& $v) {    if (Is_array ($v)) {      T ($v);    } else{      $arr [$i] = addslashes ($v);}}  T ($arr);p rint_r ($arr);

Change to?? You can do it.

Function T (& $arr) {foreach ($arr as $key = + $v) {if (is_string ($v)) {$arr [$key]=addslashes ($v);} Else{t ($arr [$key]);}}

I like messing up.

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 ', ' =>5 '); t ($arr);
Invalid argument supplied for foreach ()


Change to?? You can do it.

Function T (& $arr) {foreach ($arr as $key = + $v) {if (is_string ($v)) {$arr [$key]=addslashes ($v);} Else{t ($arr [$key]);}}

Change it a little bit more.

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 ' ', ' one ', ' + ', ' 5 ')); t ($arr); Print_r ($arr);

Thank you for the patience of some of the great gods upstairs, and the answers you have provided are correct, but they do not point to my problem. I looked at the code for a day and finally found out the problem with the code I wrote: The reason for the error is that $arr =array (' II ', array (' one ' = ' "EC" ', ' =>5 ')); 5 recursive to foreach is not an array, so the report provides a warning for the invalid parameter for foreach

$arr =array (' II ', array (' one ' = ' "EC" ', ' =>5 '));
Function T (& $arr) {
foreach ($arr as $v) {
if (is_string ($v)) {
$arr []=addslashes ($v); If you use $arr[], you can only add elements in the original array case
}else{
T ($v); Here $v print out is an array (' one ' = ' = ' EC ', ' =>5 ') when it recursively goes back to foreach inside it is not $arr original array, is a new array that is completely unrelated to $arr
}
}
}


As the following writing is OK!
$arr =array (' II ', array (' one ' = ' "EC" ', ' =>5 '));
Function T (& $arr) {
foreach ($arr as $k = = $v) {
if (is_string ($v)) {
$arr [$k]=addslashes ($v);
}else{
if (Is_array ($v)) {//Plus judgment, you can turn off warnings that have invalid parameters for foreach
T ($arr [$k]); This refers to the array (' one ' = ' EC ', ' =>5 ') of the original arrays.
}

  • 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.