How to pass a PHP array to a JavaScript function? Last Updated : 31 May, 2024 Comments Improve Suggest changes Like Article Like Report 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: $sampleArray = array( 0 => "Geeks", 1 => "for", 2 => "Geeks", );?>Using json_encode() function to retrieve the array elements var passedArray = ;Example: html // Create an array $sampleArray = array( 0 => "Geeks", 1 => "for", 2 => "Geeks", ) ?> <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: GeeksforGeeksMethod 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 = ;Example: html // 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, BoatMethod 3: Using a Hidden HTML ElementYou 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 // Creating a PHP Array $sampleArray = array('Apple', 'Banana', 'Orange'); ?> Passing PHP Array to JavaScript echo htmlspecialchars(json_encode($sampleArray)); ?>"> // 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); }); Output: Apple, Banana, OrangePHP 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. Comment More infoAdvertise with us Next Article How to pass a PHP array to a JavaScript function? N Nilarjun Follow Improve Article Tags : PHP Similar Reads How to pass an array as a function parameter in JavaScript? Here are different ways to pass an array as a function paramter in JavaScript.1. Using apply() MethodThe apply() method invokes a function with a specified `this` value and an array of arguments. It takes two parameters: the first is the `this` context for the function, and the second is an array of 2 min read How to Store an Object sent by a Function in JavaScript ? When a function in JavaScript returns an object, there are several ways to store this returned object for later use. These objects can be stored in variables, array elements, or properties of other objects. Essentially, any data structure capable of holding values can store the returned object. Tabl 2 min read How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod 4 min read How to Call a JavaScript Function from an onsubmit Event ? The onsubmit event attribute in HTML is triggered when a form is submitted. It is also useful for validating form data or performing actions before any submission and ensures better control and validation of user inputs before data is sent. The below methods can be used to call a JavaScript function 2 min read Set to Array in JS or JavaScript This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways:1. Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the 2 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read How to transform String into a function in JavaScript ? In this article, we will see how to transform a String into a function in JavaScript. There are two ways to transform a String into a function in JavaScript. The first and easy one is eval() but it is not a secure method as it can run inside your application without permission hence more prone to th 3 min read Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen 7 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read Like