Home >Backend Development >PHP Tutorial >How Can I Pass a PHP Array to a JavaScript Function?
Passing a PHP Array to a JavaScript Function
To effectively transfer data from PHP to JavaScript, consider utilizing JSON. JSON enables the conversion of PHP variables, including arrays, into a format that JavaScript can interpret.
Enhanced Code:
drawChart(600/50, <?php echo json_encode($day); ?>, ...)
Explanation:
In the PHP code block, convert the $day array to a JSON string using json_encode(). This string is then echoed into the JavaScript code, allowing you to directly assign it to a JavaScript variable within the drawChart() function.
JavaScript Usage:
var dayArray = <?php echo json_encode($day); ?>;
Considerations:
The above is the detailed content of How Can I Pass a PHP Array to a JavaScript Function?. For more information, please follow other related articles on the PHP Chinese website!