How can UTF-8 encoding be ensured when using json_encode in PHP?

When using json_encode in PHP, UTF-8 encoding can be ensured by setting the JSON_UNESCAPED_UNICODE flag in the options parameter of the json_encode function. This flag prevents non-ASCII characters from being escaped, ensuring that the output is in UTF-8 encoding.

$data = array(
    'name' => 'Jérémy',
    'age' => 30
);

$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo $json;