I have a multidimensional array, and some of the elements are objects. I want to end up with 2 arrays, one which has had all string values and object properties passed through my esc() function, and one which is the unmodified original.
Given the following code:
$raw = $data;
echo $raw['obj']->description; // Prints '< >Test Desc'
array_walk_recursive($data, function (&$item, $key){
if(is_string($item)) {
$item = esc($item);
} elseif(is_object($item)) {
foreach ($item as $property => $value) {
if(is_string($value)) {
$item->$property = esc($value);
}
}
}
});
echo $data['obj']->description; // Prints '< >Test Desc' - Correct
echo $raw['obj']->description; // Prints '< >Test Desc' - Incorrect
I would expect $raw to be entirely unmodified, and $data to have been processed through esc(). This is the case except for object properties. For some reason the object in $raw is also modified, so that the two echo
lines print different values, why is this?