Why are associative arrays automatically converted to objects in JavaScript when using JSON encoding in PHP?

When using JSON encoding in PHP, associative arrays are automatically converted to objects in JavaScript because JSON does not have a native concept of associative arrays. To preserve the associative array structure, you can use the `JSON_FORCE_OBJECT` flag when encoding the data in PHP.

$data = array("key1" => "value1", "key2" => "value2");
$json = json_encode($data, JSON_FORCE_OBJECT);
echo $json;