hi,
i have an array into PHP and i want to use it into javascript..
canany1 help my by guiding or giving me the coding ?
cz i want to processed that data on client,.....
plzreplysoon
hi,
i have an array into PHP and i want to use it into javascript..
canany1 help my by guiding or giving me the coding ?
cz i want to processed that data on client,.....
plzreplysoon
To cover all possible sorts of arrays could get complex, but this looks good for basic stuff:
http://forums.devshed.com/showpost.p...05&postcount=4
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Use PHP to echo the data in the array as text in a javascript format.
For example:
echo 'var = ['.implode(', '$array).'];';
That's a simple array... it can certainly get more complex.
A foreach loop in php can work like this:
foreach ($array as $key=>$value) {
//do whatever you want
}
$key is the key, and $value is the value.
If it needs to be multi-layered, then you'll need a function and if statements to echo strings and start a new layer of the function for arrays.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
or you can use:
echo 'var = '.json_encode($array).';';
It will work also with multi-dimension arrays.
djr33 (12-30-2009)
That looks to be very useful if it does as one might imagine. However, care should be exercised:
from:PHP 5 >= 5.2.0, PECL json >= 1.2.0
http://us2.php.net/manual/en/function.json-encode.php
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
its giving square breckets in output...
c this code
<?php
$arr = array ('a','b','c','d','e');
echo "var a =" . json_encode($arr)."";
?>
out put is:
var a =["a","b","c","d","e"]
but i want out put like this :
var a ={"a","b","c","d","e"}
so it can be used as array
plz give me some readymade coding for this
This (what you say you are getting):
is a javascript array.Code:var a =["a","b","c","d","e"]
This:
is a javascript error, an invalid object initializer.Code:var a ={"a","b","c","d","e"}
Try:
or, more properly:PHP Code:<script type="text/javascript">
<?php
$arr = array ('a','b','c','d','e');
echo "var a =" . json_encode($arr)."";
?>
;alert (a[1]);
</script>
Both work.PHP Code:<script type="text/javascript">
<?php
$arr = array ('a','b','c','d','e');
echo 'var a = ' . json_encode($arr) . ";\n";
?>
alert(a[1]);
</script>
Note: Requires:
PHP 5 >= 5.2.0, PECL json >= 1.2.0
Last edited by jscheuer1; 01-01-2010 at 03:41 PM. Reason: add demos
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
This give you javascript syntax error.
Try test this sample
{} brackets can be used only with associative array.Code:<html> <script> <?php $arr1 = array ('a','b','c','d','e'); echo "var a =" . json_encode($arr1).";"; ?> alert("a: "+a[1]); </script> </html>
NOTE:
If you don't have PHP 5 >= 5.2.0, PECL json >= 1.2.0, or want portable code, use this
Code:if (!function_exists('json_encode')){ function json_encode($a){ $str = '{'; $dlm = ''; if (is_array($a)){ foreach ($a as $key => $item){ if (is_array($item)){ $str.= $dlm.'"'.$key.'":'.my_json_encode($item).''; }else{ $str.= $dlm.'"'.$key.'":"'.$item.'"'; } $dlm = ','; } } $str .= '}'; return $str; } }
Last edited by simplecode; 01-01-2010 at 04:42 PM.
Bookmarks