serialize array with javascript (php compatible, associative arrays and mootools)

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 04-10-2009

1

Hi,

one of the task i was need to do, was to write a javascript funtion to serialize a array, so can be sent through ajax.

the task wasn’t easy, why searching on the web i only found partially working functions (some that only proccess numerical arrays, other even’t didn’t worked, and wasn’t recursive) but serching i come up with the perfect javascript serialize function ever :D

in resume it can:

  • parse numerical arrays
  • associative arrays
  • mixed arrays
  • multidimensional arrays
  • it detect if the index or values of the element it’s int or string and parse it accordingly
  • mootools compatible (read below what i mean with  this

so i dont want you wait anymore, here it’s the function:

function serializeArray(ArrayElem)
{
var cont=”";
var counts=0;
var jj;
var res = “”;

for(jj in ArrayElem)
{
//if mootools it’s included, this hack prevent that the function parse also the prototype objects
if(jj==”$family”){
res = ‘a:’+counts+’:{‘;
res += cont;
res += ‘}’;
return res;
}
if(isInt(jj)){
cont += ‘i:’+jj+’;';
}else{
cont += ’s:’+jj.length+’:”‘+jj+’”;’;
}
if(isArray(ArrayElem[jj])){
cont += serializeArray(ArrayElem[jj]);
}else{
if(isInt(ArrayElem[jj])){
cont += ‘i:’+ArrayElem[jj]+’;';
}else{
cont += ’s:’+ArrayElem[jj].length+’:”‘+ArrayElem[jj]+’”;’;
}
}
counts++;
}

res = ‘a:’+counts+’:{‘;
res += cont;
res += ‘}’;

return res;
}

function isArray(obj) {

return obj.constructor == Array;

}

function isInt(myNum) {
// get the modulus: if it’s 0, then it’s an integer
var myMod = myNum % 1;

if (myMod == 0) {
return true;
} else {
return false;
}
}

i going to explain some things about this function:

  1. the isInt() function it’s very simple, i dont know if it can fail with some weird value, but at least it’s fast and cross browser compatible
  2. i’m using a counter and not the length propertie of the array why with associative array it return 0
  3. as you see, there it’s a hack for mootools, that it’s it make it “compatible”, whiteout that hack the function also parse all the native properties and elements added at the moment when you add mootools to the document. i didn’t know any other form to stop it, so if you know something advise :D
  4. in the php that receive the serialized array, remember to use stripslashes if the magic quotes are ON and maybe you going to need to use html_entity_decode() (at least that was my case) to convert the doble quotes character to signs.

i hope it help you, it’s very easy to use and understand and can save you some hours of work :D

Regards,

Shadow

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Click here to lend your support to: Funding for Ipsilon Developments and make a donation at www.pledgie.com !

Related Post

Comments (1)

What can I say? THANKS! I’ve been looking for this function for a while, and mootools kept giving me all function prototypes. Now I finally managed to make it work thanks to your function. Thank you again :D

Write a comment