Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,959
    Thanks
    120
    Thanked 76 Times in 76 Posts

    multidim array to string

    Any script outhere that would convert:
    array like
    [[a,b,c],[d,e,f]]
    into string
    '[[a,b,c],[d,e,f]]'

    For any multi dimension ?

    BTW, as far as I see

    Code:
    eval('[[a,b,c],[d,e,f]]') returns reversed thing, that is, string to multidim array
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Not that much robust, but may be this can be helpful....
    PHP Code:
    var myArray = [[1,2,3],['a','b','c'],['f',4], [] ];

    function 
    ArrayToString(arr) {
      var 
    '';
      for(var 
    0arr.lengthi++) {
        if(
    typeof arr[i] === 'object' && arr[i].length)
          
    += ArrayToString(arr[i]) + ',';
        else if(
    typeof arr[i] === 'object' && arr[i].length === 0)
          
    += '[],';
        else if(
    isNaN(arr[i]))
          
    += "'" arr[i] + "',";
        else
          
    += arr[i] + ',';
      }
      if(
    s.length 0) {
        
    s.substr(0s.length-1);
      }
      return 
    '[' ']';
    }

    document.writeArrayToString(myArray) ); 
    Regards,
    Niral Soni

  • #3
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    A variant
    Code:
    var myArray = [[1,2,3],['a','b','c'],['f',4], [] ];
    alert(JSON.stringify(myArray))

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    I wonder what the idea behind stringifying an array is? I'd be willing to be that there is some better way of doing what you are trying to do, if you would like to give us a bit more detail.

  • #5
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,959
    Thanks
    120
    Thanked 76 Times in 76 Posts
    The only reasonis is that HTML5 sessionStorage supports only string and I need to store array there.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •