Skip to content
PHP Logo

What are the implications of passing multiple arrays to a JavaScript script in PHP?

When passing multiple arrays to a JavaScript script in PHP, you need to encode the arrays as JSON using the `json_encode` function. This ensures that the arrays are properly formatted and can be easily accessed in the JavaScript script.

php
<?php

// Sample arrays
$array1 = [1, 2, 3];
$array2 = ['a', 'b', 'c'];

// Encode arrays as JSON
$json_array1 = json_encode($array1);
$json_array2 = json_encode($array2);

// Pass JSON-encoded arrays to JavaScript
echo "<script>";
echo "var array1 = $json_array1;";
echo "var array2 = $json_array2;";
echo "</script>";

?>