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.
- Creating an array in PHP:
<?php $sampleArray = array( 0 => "Geeks", 1 => "for", 2 => "Geeks", ); ?>
- Using json_encode() function to retrieve the array elements
var passedArray = <?php echo json_encode($sampleArray); ?>
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.
var passedArray = <?php echo '["' . implode('", "', $sampleArray) . '"]' ?>;
Car, Bike, Boat
- How to pass PHP Variables by reference ?
- How to pass form variables from one page to other page in PHP ?
- How to pass integer by reference in Java
- Perl | Pass By Reference
- How to pass string to a component using Angular2 ?
- How to pass two parameters to EventEmitter in Angular 9 ?
- How to pass query parameters with a routerLink ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.