How can type-casting in PHP be used to resolve formatting issues in JSON output?

When encoding data to JSON in PHP, sometimes formatting issues can arise due to the data types of the values being encoded. Type-casting can be used to explicitly convert values to the desired data type before encoding them to JSON, ensuring that the output is correctly formatted.

$data = [
    'name' => 'John Doe',
    'age' => 30,
    'is_active' => true,
    'balance' => '100.50'
];

// Type-casting balance to float
$data['balance'] = (float) $data['balance'];

// Encoding data to JSON
$json = json_encode($data);

echo $json;