Trouble converting php array to javascript

Discussion in 'Programming' started by Sleeping Troll, Dec 13, 2008.

  1. #1
    I am using the following to convert a php array to javascript.

     
    
    var TextsArray = new Array(<?php
    
                  foreach($TextsArray as $Text)
    
                      {
    
                       echo "'$Text',";
    
                      }
    
                  ?>);
    
    
    
    
    PHP:


    The output of this statement is:



     
    
    var TextsArray = new Array('Your Text Here',);
    
    
    Code (markup):


    This generated line creates a syntax error, is it because of the single quotes???
     
    Sleeping Troll, Dec 13, 2008 IP
  2. electricshoe

    electricshoe Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No it's because of the comma. You need to check if you're on the last item of the array in php and not put the comma in. Just start an int counter and compare it to the length of the php array, if they are equal, don't echo the comma.
     
    electricshoe, Dec 13, 2008 IP