Natavar Ghodasara
Natavar Ghodasara

Reputation: 146

Get Javascript variable as an index with PHP array in Javascript

Want javascript variable as index variable in php array in javascript.

Code is not working .....

<script>
     var total = <?php echo count($resvalue); ?> ; //$resvalue is an array holding the values are 1,2,3,4. total will hold count of that array.
     for(var j=0;j<total;j++)
     {
          <php echo $resvalue[j]; ?> // where j is javascript variable and $resvalue is PHP array
     }
 </script>

Upvotes: 0

Views: 1370

Answers (1)

Eric Herlitz
Eric Herlitz

Reputation: 26267

You cannot read values in a php array from javascript. Instead echo the array and turn it into a javascript array and let javascript do the count.

<script>
     var resvalue = [<?php echo $resvalue; ?>]; // or something like this
     for(var j=0; j < resvalue.length; j++)
     {
          // your value is available in the js-array
          // resvalue[j]
     }
</script>

Upvotes: 2

Related Questions