Your Ad Here

Monday, October 3, 2011

PHP to JAVASCRIPT: 2D Array Conversion


<?php


// start off the javascript array stirng
$js_array 'var pf = [';


// loop into the first dimension
for($i 0$ic = (count($pf)-1); $i $ic$i++)

{

    // add a new javascript array to the js_stirng

    $js_array .= '[';

    

    // loop through the second dimension

    for($x 0$xc = (count($pf[$i])-1); $x $xc$x++)

    {

        // add the data to the js_array

        $js_array .= '"'.$pf[$i][$x].'"';

        

        // if its not the last peice of data, add a comma

        if($x != $xc)

            $js_array .= ',';

    }

    

    // end the javascript array

    $js_array .= ']';

    

    // if its not the last peice of data, add a comma

    if($i != $ic)

        $js_array .= ',';

}


// finish the js_array string
$js_array .= '];';


PHP to JAVASCRIPT: 2D Array Conversion// now just print the js
?>


<script language="javascript">

<!--

<?=$js_array?>
//-->

</script>

No comments:

Post a Comment