How can JSON data be effectively used to transfer PHP array data to JavaScript variables?

To transfer PHP array data to JavaScript variables, you can encode the PHP array into JSON format using the `json_encode()` function in PHP. Then, you can output the JSON data into a JavaScript variable within a script tag in your HTML document. This allows you to easily access and manipulate the PHP array data in your JavaScript code.

<?php
// Sample PHP array data
$phpArray = array("name" => "John", "age" => 30, "city" => "New York");

// Encode PHP array into JSON format
$jsonData = json_encode($phpArray);
?>

<script>
// Output JSON data into a JavaScript variable
var jsArray = <?php echo $jsonData; ?>;

// Access and use the PHP array data in JavaScript
console.log(jsArray.name); // Output: John
console.log(jsArray.age); // Output: 30
console.log(jsArray.city); // Output: New York
</script>