How to pass a PHP array to a JavaScript function?

Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON).

Method 1: Using json_encode() function:

The

json_encode()

function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.

Steps:

  • Creating an array in PHP:
    <?php
    $sampleArray = array(
    0 => "Beginner",
    1 => "for",
    2 => "Beginner",
    );
    ?>
  • Using json_encode() function to retrieve the array elements
    var passedArray = <?php echo json_encode($sampleArray); ?>;

Example:

html
<?php
   
// Create an array
$sampleArray = array(
    0 => "Beginner", 
    1 => "for", 
    2 => "Beginner", 
)
?>
 
<script>

// Access the array elements
var passedArray = 
    <?php echo json_encode($sampleArray); ?>;
     
// Display the array elements
for(var i = 0; i < passedArray.length; i++){
    document.write(passedArray[i]);
}
</script>

Output:

w3wiki

Method 2: Using PHP implode() function:

The implode() is used to join the elements of an array. The implode() function is the alias of join() function and works exactly same as that of join() function. The implode() function is used to build a string that becomes an array literal in JavaScript. So, if we have an array in PHP, we can pass it to JavaScript as follows:

var passedArray = <?php echo '["' . implode('", "', $sampleArray) . '"]' ?>;

Example:

html
<?php

// Creating a PHP Array
$sampleArray = array('Car', 'Bike', 'Boat');

?>

<script type="text/javascript">
 
// Using PHP implode() function
var passedArray = 
    <?php echo '["' . implode('", "', $sampleArray) . '"]' ?>;
 
// Printing the passed array elements
document.write(passedArray);
 
</script>

Output:

Car, Bike, Boat

Method 3: Using a Hidden HTML Element

You can embed the PHP array directly into the HTML and access it from JavaScript by reading the value of a hidden input element.

In this approach, we encode the PHP array as JSON and embed it into a hidden input element in the HTML. Then, in the JavaScript section, we retrieve the value of the hidden input and parse it back to a JavaScript array using JSON.parse(). Finally, we can use the array as needed in JavaScript.

PHP
<?php
// Creating a PHP Array
$sampleArray = array('Apple', 'Banana', 'Orange');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Passing PHP Array to JavaScript</title>
</head>
<body>

<!-- Embedding PHP array as JSON in a hidden input -->
<input type="hidden" id="phpArray" value="<?php echo htmlspecialchars(json_encode($sampleArray)); ?>">

<script>
// Accessing the PHP array in JavaScript
var passedArray = JSON.parse(document.getElementById('phpArray').value);
   
// Printing the passed array elements
passedArray.forEach(function(item) {
    console.log(item);
});
</script>

</body>
</html>

Output:

Apple, Banana, Orange

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.