Example #1
0
function recursive_array_replace($find, $replace, $data)
{
    if (is_array($data)) {
        foreach ($data as $key => $value) {
            if (is_array($value) || is_object($value)) {
                $data[$key] = recursive_array_replace($find, $replace, $value);
            } else {
                $data[$key] = str_replace($find, $replace, $value);
            }
        }
    } else {
        if (is_object($data)) {
            foreach ($data as $key => $value) {
                if (is_array($value || is_object($value))) {
                    $data->{$key} = recursive_array_replace($find, $replace, $value);
                } else {
                    $data->{$key} = str_replace($find, $replace, $value);
                }
            }
        } else {
            $data = str_replace($find, $replace, $data);
        }
    }
    return $data;
}
function recursive_array_replace($find, $replace, &$data)
{
    if (is_array($data)) {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                recursive_array_replace($find, $replace, $data[$key]);
            } else {
                // have to check if it's string to ensure no switching to string for booleans/numbers/nulls - don't need any nasty conversions
                if (is_string($value)) {
                    $data[$key] = str_replace($find, $replace, $value);
                }
            }
        }
    } else {
        if (is_string($data)) {
            $data = str_replace($find, $replace, $data);
        }
    }
}