PDA

View Full Version : PHP array into javascript array



gurmeet
12-27-2009, 09:02 AM
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

jscheuer1
12-27-2009, 12:04 PM
To cover all possible sorts of arrays could get complex, but this looks good for basic stuff:

http://forums.devshed.com/showpost.php?p=255405&postcount=4

djr33
12-27-2009, 05:06 PM
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.

simplecode
12-30-2009, 10:14 PM
or you can use:

echo 'var = '.json_encode($array).';';

It will work also with multi-dimension arrays.

jscheuer1
12-31-2009, 07:14 AM
or you can use:

echo 'var = '.json_encode($array).';';

It will work also with multi-dimension arrays.

That looks to be very useful if it does as one might imagine. However, care should be exercised:


PHP 5 >= 5.2.0, PECL json >= 1.2.0

from:

http://us2.php.net/manual/en/function.json-encode.php

gurmeet
01-01-2010, 02:47 PM
or you can use:

echo 'var = '.json_encode($array).';';

It will work also with multi-dimension arrays.


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

jscheuer1
01-01-2010, 03:26 PM
This (what you say you are getting):



var a =["a","b","c","d","e"]

is a javascript array.

This:



var a ={"a","b","c","d","e"}

is a javascript error, an invalid object initializer.

Try:


<script type="text/javascript">
<?php
$arr = array ('a','b','c','d','e');

echo "var a =" . json_encode($arr)."";
?>
;alert (a[1]);
</script>

or, more properly:


<script type="text/javascript">
<?php
$arr = array ('a','b','c','d','e');
echo 'var a = ' . json_encode($arr) . ";\n";
?>
alert(a[1]);
</script>

Both work.

Note: Requires:

PHP 5 >= 5.2.0, PECL json >= 1.2.0

simplecode
01-01-2010, 03:33 PM
but i want out put like this :
var a ={"a","b","c","d","e"}
so it can be used as array

This give you javascript syntax error.

Try test this sample

<html>
<script>
<?php
$arr1 = array ('a','b','c','d','e');
echo "var a =" . json_encode($arr1).";";
?>
alert("a: "+a[1]);
</script>
</html>

{} brackets can be used only with associative array.

NOTE:
If you don't have PHP 5 >= 5.2.0, PECL json >= 1.2.0, or want portable code, use this



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;
}
}