How to convert PHP array to JavaScript array

Today we’ll explain to you how to convert PHP array to JavaScript array. Sometimes, we need to pass a PHP array in JavaScript that can be a single or multidimensional or indexed or associative array. Mostly, we pass PHP data in an Ajax request, at that time it is useful.

Using the json_encode() function, we can easily convert a PHP array to a JavaScript array and it is accessible in JavaScript.

Convert the different types of an array

  1. Single dimensional indexed array
  2. Multidimensional indexed array
  3. Multidimensional associative array

Let’s start with examples.

1. Single dimensional indexed array

Here, we will convert the PHP numeric array to JavaScript array as shown below in the code.

PHP

JavaScript

2. Multidimensional indexed array

Now, we will take an example to convert a PHP multidimensional indexed array to a JavaScript array.

PHP

JavaScript

3. Multidimensional associative array

Lastly, let’s take an example to convert a PHP multidimensional associative array to a JavaScript array.

PHP

JavaScript

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *