Formatting serializeArray
<p>jQuery.serializeArray currently returns this result:</p>

<pre><code>[Object { name="id",  value="0"}, Object { name="gender[]",  value="1"}, Object { name="gender[]",  value="2"}, Object { name="countries[]",  value="AU"}, Object { name="countries[]",  value="GB"}, Object { name="ages[]",  value="25"}, Object { name="ages[]",  value="99"}]
</code></pre>

<p>I'd like to get result formatted on this way:</p>

<pre><code>{id: 0, gender: ['1', '2'], countries: ['AU', 'GB'], ages: ['25', '99']}
</code></pre>

<p>How to achieve this?</p>

<p>I didn't like how serializeArray format its output and I created function which formats it on more acceptable way.</p>

<pre><code>(function( $ ){
$.fn.serializeJSON=function() {
    var json = {}, propname;
    $.each($(this).serializeArray(), function(index, n){
        propname = n.name.replace('[]', '');
        if (!json.hasOwnProperty(propname))
        {
            json[propname] = n.value;
        }
        else if( ! (json[propname] instanceof Array) )
        {
            json[propname] = new Array(json[propname], n.value);
        }
        else
        {
            json[propname].push(n.value);
        }
    });
    return json;
}; 
})(jQuery);
</code></pre>

<p>After form is created just put somewhere this piece of code:</p>

<pre><code>$('#form_id').submit(function(e) {
    e.preventDefault();
    e.stopPropagation();
    var data = $(this).serializeJSON();
    console.log(data);
})
</code></pre>

<p>I hope it helps to ones who like how form is posted to PHP.</p>
