Converting PHP Array into JavaScript Array Tutorial

In this tutorial, we will tackle about how to Convert PHP Array into JavaScript Array. This tutorial aims to teach new programmers or IT/CS students how to pass array of data from PHP to JavaScript. This topic is sort of similar to our past topic which is Passing PHP Variables into JavaScript Tutorial but this time we'll be passing an array of data and convert it into a JS Array using the 2 best and straight forward way that I can think of.

Why do you need to Convert PHP Array into JavaScript Array?

Convertin PHP Array into JS Array is not a requirement for developing an application. Although, you will need to convert the PHP Arrays to JS for some instances such as the following:

  • You need to dynamically manage and control the array of data from your database into the client-side
  • Use the array data when executing a JS Function
  • and more

These are just some of the reason why you need to convert your PHP Array of data to JS Array. Using the converted array data, you will have more options to control the data to give your end-users a better experience while using your application.

How to Convert PHP Array into JavaScript Array?

There are a lot of ways of converting your Array of data from PHP to JavaScript. Before the conversion, you must Pass the PHP Array Variable first into the JavaScript.

2 ways to Convert PHP array to JS array

Here are the 2 best ways to convert an array of data from PHP to JS. These techniques are commonly used and more effective.

  1. Using JS Map Method
  2. Using JS For Loop Method

Using JS Map Method

JS Map Method creates a new array by calling a function in each value of your current array. The following snippets are an example of converting an array of data from PHP to JS using the said method.

PHP Script

  1. <?php
  2. $sample_arr1 = [
  3. "Item 1",
  4. "Item 2",
  5. "Item 3",
  6. "Item 4",
  7. "Item 5"
  8. ];
  9.  
  10. $sample_arr2 = [
  11. "Value 1",
  12. "Value 2",
  13. "Value 3",
  14. "Value 4",
  15. "Value 5"
  16. ];
  17. ?>
  18.  

JS Script

  1. var arr1 = [],
  2. arr2 = [];
  3.  
  4. var obj1 = JSON.parse('<?= json_encode($sample_arr1) ?>'),
  5. obj2 = JSON.parse('<?= json_encode($sample_arr2) ?>');
  6.  
  7. Object.keys(obj1).map(k =>{
  8. arr1.push(obj1[k])
  9. })
  10. console.log("Sample Array #1:", arr1)
  11.  
  12. Object.keys(obj2).map(k =>{
  13. arr2.push(obj2[k])
  14. })
  15. console.log("Sample Array #2:", arr2)
  16.  

Or

  1. var arr1 = [],
  2. arr2 = [];
  3.  
  4. var obj1 = JSON.parse('<?= json_encode($sample_arr1) ?>'),
  5. obj2 = JSON.parse('<?= json_encode($sample_arr2) ?>');
  6.  
  7. arr1 = Object.keys(obj1).map(k => { return obj1[k] });
  8. console.log("Sample Array #1:", arr1)
  9.  
  10. arr2 = Object.keys(obj2).map(k => { return obj2[k] });
  11. console.log("Sample Array #2:", arr2)
  12.  

Using JS For Loop Method

The other way to convert an Array of data from PHP to JS is by using For Loop. Using this, you can store the object data into an array variable by iteration. See the sample snippet below.

PHP Script

  1. <?php
  2. $sample_arr1 = [
  3. "Item 1",
  4. "Item 2",
  5. "Item 3",
  6. "Item 4",
  7. "Item 5"
  8. ];
  9.  
  10. $sample_arr2 = [
  11. "Value 1",
  12. "Value 2",
  13. "Value 3",
  14. "Value 4",
  15. "Value 5"
  16. ];
  17. ?>
  18.  

JS Script

  1. var arr1 = [],
  2. arr2 = [];
  3.  
  4. var obj1 = JSON.parse('<?= json_encode($sample_arr1) ?>'),
  5. obj2 = JSON.parse('<?= json_encode($sample_arr2) ?>');
  6.  
  7. for(var i = 0; i < Object.keys(obj1).length; i++){
  8. arr1.push(obj1[i])
  9. }
  10. console.log("Sample Array #1:", arr1);
  11.  
  12. for(var i = 0; i < Object.keys(obj2).length; i++){
  13. arr2.push(obj2[i])
  14. }
  15. console.log("Sample Array #2:", arr2);
  16.  

DEMO VIDEO

That is! You can try the solutions on your end to see how it works. I also provided a simple program that demonstrates how to convert PHP Array into JS Array. The download button is located below this article.

I hope this tutorial will help you with what you are looking for and you'll find this useful for your current and future projects. Explore more on this website for more tutorials and Free Source Codes.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
6 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.