Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stripslashes all the items of an array at once?

Tags:

php

I need to stripslashes all items of an array at once.

Any idea how can I do this?

like image 774
Starx Avatar asked Aug 06 '10 08:08

Starx


People also ask

How do you remove slashes from an array?

You can use json_decode() to conrect received json string into multidimensional array then you can use json_encode() to print that array in json format without any slashes.

What is strip slashes?

The stripslashes() function removes backslashes added by the addslashes() function. Tip: This function can be used to clean up data retrieved from a database or from an HTML form.

How to remove backslashes in the string in PHP?

The stripslashes() function is a built-in function in PHP. This function removes backslashes in a string. Parameter: This function accepts only one parameter as shown in the above syntax.


2 Answers

foreach ($your_array as $key=>$value) {
$your_array[$key] = stripslashes($value);
}

or for many levels array use this :

function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}


$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);

print_r($array);
like image 187
Gatman Avatar answered Oct 09 '22 15:10

Gatman


For uni-dimensional arrays, array_map will do:

$a = array_map('stripslashes', $a);

For multi-dimensional arrays you can do something like:

$a = json_decode(stripslashes(json_encode($a)), true);

This last one can be used to fix magic_quotes, see this comment.

like image 23
Alix Axel Avatar answered Oct 09 '22 16:10

Alix Axel