How to convert PHP array to JavaScript array

In this tutorial, we are going to learn that how we convert PHP array to javascript array First, what you have to do is to convert PHP array to JSON format using json_encode() function which was available in PHP.

PHP array converts to JavScript array and accessible in JavaScript. whatever the array type is a single or multidimensional or indexed or associative array. As we know, most of the PHP API using JSON encoded to data to send from one server to other servers.Because JSON is fast.

First of All, We understand that there are mainly two types of an array which we are going to discuss now.First one is a single dimension array and the second one is a multidimensional array.

convert php aray to javascript array
convert php array to javascript array

Convert Php Array to JavaScript Array

Array Types:

1. Single Dimension Array

2. Multi Dimension Array

Single Dimension Array:

Single Dimension array is an empty space for storing the value. In this, we can store string, int, float. Every value store on a particular index which ranges from 0 to max value stored.

<?php
   var $itemsarray= array("Apple", "Bear", "Cat", "Dog");
?>

<script>
   var items= <?php echo json_encode($itemsarray); ?>;
   console.log(items[2]); // Output: Bear
   // OR
   alert(items[0]); // Output: Apple
</script>

In above example, we can see there is a $itemsarray is holding a 4 value on there 4 indexes. At 0 index there is “Apple” on the second index there is a “Bear”  on the third index there is a “Cat” on the fourth index there is a “Dog”.

$itemsarray[0]=Apple

$itemsarray[1]=Bear

$itemsarray[2]=Cat

$itemsarray[3]=Dog

MultiDimensional Array:

A multidimensional array is an empty space for storing the array. In this, we can store string, int, float. Every array store on a particular index which ranges from 0 to max value stored. In this, every index contains a specific array.

<?php
   var $itemsarray= array( 
                     array('name'='Apple', 'price'=>'12345'),
                     array('name'='Bear', 'price'=>'13344'),
                     array('name'='Potato', 'price'=>'00440')
                   );
?>

<script>
   var items= <?php echo json_encode($itemsarray); ?>;
   console.log(items[2][name]); // Output: Bear
   // OR
   alert(items[0][price]); // Output: Apple
</script>

In above example, we can see there is a $itemsarray is holding a 3 array on there 3 indexes. At 0 index there is “Apple” and “12345” on the second index there is a “Bear” and “13344”  on the third index there is a “Cat” and “004400”.

Also Read:

  1.  PHP Tutorial – PHP Introduction
  2.  Explode And Implode Function In Php

Conclusion:

In this tutorial, we can see how we can use a single and multidimensional array and convert PHP array to a javascript array.

If you have any query related to this post feel free to comment on the comment box. Do not forget to like and share this post because it helps we create more awesome. And don’t forget to like on Facebook Page.

Leave a Reply

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