help with: Javasript arrays using .split()

Discussion in 'Web Design and Development' started by yagran, Sep 22, 2008.

  1. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #1
    Hi, i've got myself really confuse with this all now and i could do with an outsider having a look...

    I am plotting graphs using Flot for jQuery, it likes its data set in the following way:
    Code:
    var d1 = [[1224690646000, 4], [1224690663000, 4], [1224690672000, 1], [1224693751000, 4], [1224693166000, 4]]
    where each item of the aray has an x any y coordinate.

    I have a data set which is created by php and written to a text file like this:
    Code:
    1224690646000, 4+1224690663000, 4+1224690672000, 1+1224693751000, 4+1224693166000, 4
    And then im using a xmlhttprequest to grab the textfile and put it into the dataText variable, then im trying to split that into an array ready to plot:
    Code:
    dataText = oXML.responseText;
    dataTextSplit = dataText.split("+");
    Its not working right, and im pretty sure its because im not splitting the text file into the array properly but i cant get my head around how it should be done?

    any help would be truly fantastic!
     
  2. bootedbear macrumors 6502

    bootedbear

    Joined:
    Sep 13, 2004
    Location:
    Austin, TX
    #2
    I assume you can't just fix the php script to return the proper JSON format?
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    That code will only give you a single dimension array rather than a 2D array like you want. If I get a chance later I'll give you some code for it, but don't have time at the moment. Hopefully that hint will get you in the right direction though.
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Well guess no one else chimed in. Here's a solution:

    PHP:
    var dataText '1224690646000, 4+1224690663000, 4+1224690672000, 1+1224693751000, 4+1224693166000, 4';
    var 
    tmp dataText.split('+');
    var 
    dataTextSplit = new Array();
    for (var 
    x=0y=tmp.lengthx<yx++) {
      
    dataTextSplit[x] = tmp[x].split(', ');
    }
    You of course would need to swap in your oXML.responseText part at the top instead of the raw data. I just used it for testing purposes. Enjoy.
     
  5. yagran thread starter macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #5
    Thank you, you're a godsend!
    Arrays do my head in!
    had to make a quick change (global variables were needed), but other than that absolutely perfect!

    thanks to everyone else that chipped in too!
     

Share This Page