Voting

: min(five, eight)?
(Example: nine)

The Note You're Voting On

Oscar Broman
12 years ago
Walk through nested arrays/objects and utf8 encode all strings.

<?php
// Usage
class Foo {
public
$somevar = 'whoop whoop';
}

$structure = array(
'object' => (object) array(
'entry' => 'hello wörld',
'another_array' => array(
'string',
1234,
'another string'
)
),
'string' => 'foo',
'foo_object' => new Foo
);

utf8_encode_deep($structure);

// $structure is now utf8 encoded
print_r($structure);

// The function
function utf8_encode_deep(&$input) {
if (
is_string($input)) {
$input = utf8_encode($input);
} else if (
is_array($input)) {
foreach (
$input as &$value) {
utf8_encode_deep($value);
}

unset(
$value);
} else if (
is_object($input)) {
$vars = array_keys(get_object_vars($input));

foreach (
$vars as $var) {
utf8_encode_deep($input->$var);
}
}
}
?>

<< Back to user notes page

To Top