PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




Array values from php to javascript


Tutorials Php

Topic

How to carry an array value from php to javascript?



Explanation

At some point of time we will be having a requirement to pass values from php array to a javascript array. For example let us consider a situation where we read some parameter from a file and use it in javascript. Here is how we can pass the values.
Follow the steps to do it yourself


Step 1:

We read a file "im.txt" as array. The file will be read in such a way that each line will be a value in array. So if we have 10 lines in "im.txt" we will have the array length as 10.

<script language="javascript">

scriptAr = new Array(); // initializing the javascript array
<?php
$file = "im.txt";
$lines = file($file); // read file values as array in php
$count = count($lines); //this gives the count of array

//In the below lines we get the values of the php array one by one and update it in the script array.
foreach ($lines as $line_num => $line)
{
print "scriptAr.push(\"$line\" );"; // This line updates the script array with new entry
}

?>

</script>


Note: Please note that the php tags are inside the javascript tag.





A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting