How can you ensure that the input data for json_encode() is in UTF-8 encoding?

When using json_encode() in PHP, it is important to ensure that the input data is in UTF-8 encoding to avoid potential encoding issues. One way to ensure this is by converting the input data to UTF-8 using the mb_convert_encoding() function before passing it to json_encode(). This will help ensure that the data is properly encoded and can be safely encoded into a JSON string.

// Ensure input data is in UTF-8 encoding before using json_encode()
$inputData = array("name" => "John Doe", "age" => 30);
$inputDataUtf8 = array_map(function($value) {
    return mb_convert_encoding($value, 'UTF-8', 'UTF-8');
}, $inputData);

$jsonString = json_encode($inputDataUtf8);
echo $jsonString;