I have a question about the following code:
PHP function, takes in an array, recursively cycles through each level, does a string replace if single quote found and returns back array.
function escape_quote($data)
{
$clean = array();
foreach ($data as $key=>$val)
{
if (is_array($val))
{
$clean[$key] = escape_quote($val);
} else {
$val = str_replace("'", "''", $val);
$clean[$key] = $val;
}
}
return $clean;
}
Here is the array of data I am passing:
$vars = array
(
'customer1' => array
(
'fname' => 'John',
'lname' => "D'oe"
),
'customer2' => array
(
'name' => array
(
'fname' => 'John',
'lname' => "D'oe"
),
'address' => '1234 street',
'phone' => '1234567899'
)
);
I make this call to the function and pass the array to it:
$output = escape_quote($vars);
print_r($output);
My output on the screen:
Array ( [customer1] => Array ( [fname] => John [lname] => D''oe ) [customer2] => Array ( [name] => Array ( [fname] => John [lname] => D'oe ) [address] => 1234 street [phone] => 1234567899 ) )
My questions is why am I not getting the second "D'oe" returned as "D''oe"
if I echo out each value in the function rather than add to the clean array it will add the second "'" to both "D'oe". I am not sure what is missing... please help.
UPDATE:
I figured out the issue, made update to code. If the $val was an array I wasn't setting the $clean[$key] back equal to escape_quote($val).