What data format should be used to ensure compatibility between PHP and JavaScript when passing arrays?

To ensure compatibility between PHP and JavaScript when passing arrays, it is recommended to use JSON (JavaScript Object Notation) as the data format. JSON is a lightweight data interchange format that is easy for both PHP and JavaScript to work with. By encoding arrays as JSON strings in PHP and decoding them in JavaScript, you can seamlessly pass array data between the two languages.

<?php

// Sample array to be passed from PHP to JavaScript
$data = array('name' => 'John', 'age' => 30, 'city' => 'New York');

// Encode the array as a JSON string
$json_data = json_encode($data);

// Output the JSON string
echo "<script> var jsonData = JSON.parse('".$json_data."'); </script>";

?>