Escape array Solution with recursive addslashes function

Source: Internet
Author: User
Escaping an array with a recursive addslashes function
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\\\ "
)

------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 "
";
Print_r ($arr);
Echo '
';

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

)


------Solution--------------------
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 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);
  • 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.