0

I would like to concatenate a php array into javascript array. And use all of this to create jquery function. Help me...

Instructions for in the loop are => descriptif[i] = .$description[i].;

    <?php echo '<script> var descriptif = new Array (); 
                for(i=0 ; i<16 ; i++)
                {
                    descriptif[i] = "'.$description["'"+ i +"'"].'";
                }   
                </script>';
    ?>

2 Answers2

0

I guess you're thinking about:

<script> var descriptif = new Array (); 
<?php for(i=0 ; i<16 ; i++) {
    echo "descriptif[$i] = '$description[$i]';"
} ?>
</script>

It will print:

<script> var descriptif = new Array (); 
    descriptif[0] = value1;
    descriptif[1] = value2;
    descriptif[2] = value3;
    ...
</script>

PS if you want to dynamize something (javascript, css ,etc.) with a scripting language (php, jsp), use , <% %> only in the parts that differ (the loop part). The rest is the same so it doesn't have to be echoed - it makes your code less clear.

ducin
  • 25,621
  • 41
  • 157
  • 256
0

You cannot access PHP arrays using client-side Javascript.

  • PHP is a server-side language. Everything happens on the server.
  • Javascript is a client-side language. Everything happens in the browser.
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52