Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: multidim array to string

1. ## 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`

• 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 s = '';  for(var i = 0; i < arr.length; i++) {    if(typeof arr[i] === 'object' && arr[i].length)      s += ArrayToString(arr[i]) + ',';    else if(typeof arr[i] === 'object' && arr[i].length === 0)      s += '[],';    else if(isNaN(arr[i]))      s += "'" + arr[i] + "',";    else      s += arr[i] + ',';  }  if(s.length > 0) {    s = s.substr(0, s.length-1);  }  return '[' + s + ']';}document.write( ArrayToString(myArray) );  ```
Regards,
Niral Soni

• A variant
Code:
```var myArray = [[1,2,3],['a','b','c'],['f',4], [] ];

• 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.

• The only reasonis is that HTML5 sessionStorage supports only string and I need to store array there.

•

#### Posting Permissions

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