Voting

: four minus zero?
(Example: nine)

The Note You're Voting On

zoranbankovic at gmail dot com
13 years ago
If someone wants to add slashes to multidimensional array directly, can use recursive (pass-by-reference) function like this:
<?php
function slashit(&$aray, $db_link)
{
foreach (
$aray as $key => &$value)
if(
is_array($value)) slashit($value, $link);
else
$aray[$key] = mysql_real_escape_string($value, $db_link);
}

// Test:
$fruits = array (
"fruits" => array("a" => "or'ange", "b" => "ban'ana", "c" => "apple'"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("fir'st", 5 => "sec'ond", "thir'd"),
"odma" => "jugo'slavija"
);

// You have to make link to the database or can use addslashes instead of mysql_real_escape_string and remove $link from function definition

slashit($fruits, $dbLink);
echo
"<pre>"; print_r($fruits); echo "</pre>";
?>

// Output:
Array
(
[fruits] => Array
(
[a] => or\'ange
[b] => ban\'ana
[c] => apple\'
)

[numbers] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)

[holes] => Array
(
[0] => fir\'st
[5] => sec\'ond
[6] => thir\'d
)

[odma] => jugo\'slavija
)

<< Back to user notes page

To Top