Simple Example of Serialize of Object/Array in PHP and jQuery

This simple tutorial help to serialize array and objects into jQuery and vise versa into PHP. PHP has in-built serialize and unserialize methods for serialize and deserialize object.

Serialization of object or php serialize array is very important operation in now days, because we are designing more responsive web application/mobile application. We need serialized array/object for ajax request as well as for web service. The target object can be JSON data object or simple form element. Serialization is used to storing or passing object values around without losing their type and structure.
The jQuery Serialize methods convert them into string and send as query string to server side. You can use de-serialization of string then you will get your own original object. There are many ways in jQuery to convert object in serialized object.

Simple Example of jQuery serialize Form to php array

jQuery providing many methods for serialize object and into array.There are specific method for each serialize types.You can use serialize(), serializeArray() and jQuery.param().

There are following method is used for serialization:

1 – serialize() : jQuery form serialize into Object

This is common method to use send form values as a serialized object to server. The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls.

$('#form').serialize()

This method send to server an object and server will received as below string :

"param1=someVal¶m2=someOtherVal"

2 – serializeArray() : jQuery form serialize into Array

This method behaves as like names, it is converting object in array manner. The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls.

$('#form').serializeArray()

This method send to server an object and server will received as below string :

{
    name: "param1",
    value: "someVal"
  },
  {
    name: "param2",
    value: "someOtherVal"
  },

3 – jQuery.param() : jQuery form concatenate and serialize into Object

Sometimes when you do an Ajax form post in jQuery, you need to merge another object into your post data. The solution is to serialize the existing form data using jQuery’s $().serialize method. Then we use the $.param() utility method to create a serialized version of any JavaScript object or array. Simply concatenate the two objects.

data = $('#form').serializeArray()+ '&' + $.param('name':'phpflow');

This method send to server a object and server will received as below string :

a=%5Bparam1%5D&b=someVal¶m2%5D=someOtherVal&name='phpflow' 

How to get serialize values in php:

parse_str() php method is use to convert query string into php array.serialize() method is use for serialize object and unserialize() for deserialize object.

$params = array();
parse_str($_GET, $params);
print_r($searcharray); 
// You get any same of that

 Array (
 [param1] => someVal
 [param2] => param2
 )
These functions return a string representations of an array or an object which can than be decoded back into array/object.

One thought on “Simple Example of Serialize of Object/Array in PHP and jQuery

Comments are closed.