note 38874 modified in faq.html by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:55:20 +0000 |
| Subject: | note 38874 modified in faq.html by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79997@lists.php.net to get a copy of this message | ||
I thought this might be useful to fellow PHP heads like myself out there.
I recently came across a need to transfer full fledged mutli-dimensional arrays from PHP to
JAVASCRIPT.
So here it is and hopefuly good things come from it.
<?php
function phparray_jscript($array, $jsarray)
{
function loop_through($array,$dimen,$localarray)
{
foreach($array as $key => $value)
{
if(is_array($value))
{
echo ($localarray.$dimen."[\"$key\"] = new Array();\n");
loop_through($value,($dimen."[\"".$key."\"]"),$localarray);
}
else
{
echo ($localarray.$dimen."[\"$key\"] = \"$value\";\n");
}
}
}
echo "<script language=\"Javascript1.1\">\n";
echo "var $jsarray = new Array();\n";
loop_through($array,"",$jsarray);
echo "</script>";
}
?>
--was--
I thought this might be useful to fellow PHP heads like myself out there.
I recently came across a need to transfer full fledged mutli-dimensional arrays from PHP to
JAVASCRIPT.
So here it is and hopefuly good things come from it.
function phparray_jscript($array, $jsarray)
{
function loop_through($array,$dimen,$localarray)
{
foreach($array as $key => $value)
{
if(is_array($value))
{
echo ($localarray.$dimen."[\"$key\"] = new Array();\n");
loop_through($value,($dimen."[\"".$key."\"]"),$localarray);
}
else
{
echo ($localarray.$dimen."[\"$key\"] = \"$value\";\n");
}
}
}
echo "<script language=\"Javascript1.1\">\n";
echo "var $jsarray = new Array();\n";
loop_through($array,"",$jsarray);
echo "</script>";
}
http://php.net/manual/en/faq.html.php