PHP Serialize String and JSON Object

I’m not really understand difference between PHP serialize string and JSON object.

I notice PHP when stored data inside database is in PHP serialize string format. Serialize string is only available in PHP, hence it is not interoperable with NodeJS.

Hence, I did simple comparison for better understanding in order to make my NodeJS can read and write PHP data and vice versa.

PHP Array Conversion to PHP Serialize String and JSON Object

//convert PHP array to serialize string and JSON object
$array = array( '1' => 'elem 1', '2'=> 'elem 2', '3'=>' elem 3');


$serialized = serialize($array);
print_r($serialized);
//expected output - serialize string: a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}


$json = json_encode($array);
print_r($json);
//expected output - JSON object: {"1":"elem 1","2":"elem 2","3":" elem 3"}

//convert back to PHP array

$unserialized = unserialize($serialized);
print_r($unserialized);
//expected output - unserialized: Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 )

$decoded1 = json_decode($json);
print_r($decoded1);
//expected output - decoded into PHP object: stdClass Object ( [1] => elem 1 [2] => elem 2 [3] => elem 3 )


$decoded2 = json_decode($json, true);
print_r($decoded2);
//expected output - decoded into PHP array: Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 )

PHP Object Conversion to PHP Serialize String and JSON Object

$object = new stdClass();
$object->name = 'Here we go';
$object->message = 'Hello world';


$serialized = serialize($object);
print_r($serialized);
//expected output - serialize string: O:8:"stdClass":2:{s:4:"name";s:10:"Here we go";s:7:"message";s:11:"Hello world";}
 

$json = json_encode($object);
print_r($json);
//expected output - json object: {"name":"Here we go","message":"Hello world"}

$unserialized = unserialize($serialized);
print_r($unserialized);
//expected output - unserialized: stdClass Object ( [name] => Here we go [message] => Hello world )

$decoded = json_decode($json);
print_r($decoded);
//expected output - decoded JSON: stdClass Object ( [name] => Here we go [message] => Hello world )

$decoded2 = json_decode($json, true);
print_r($decoded2);
//expected output - decoded JSON: Array ( [name] => Here we go [message] => Hello world )

PHP Array of Object Conversion to PHP Serialize String and JSON Object

$object1 = new stdClass();
$object1->color = 'blue';
$object1->type = 'suv';

$object2 = new stdClass();
$object2->color = 'white';
$object2->type = 'mpv';

$arrObjects = array($object1, $object2);


$serialized = serialize($arrObjects);
print_r($serialized);
//expected output - serialize: a:2:{i:0;O:8:"stdClass":2:{s:5:"color";s:4:"blue";s:4:"type";s:3:"suv";}i:1;O:8:"stdClass":2:{s:5:"color";s:5:"white";s:4:"type";s:3:"mpv";}}


$json = json_encode($arrObjects);
print_r($json);
//expected output - JSON: [{"color":"blue","type":"suv"},{"color":"white","type":"mpv"}]
 

$unserialized = unserialize($serialized);
print_r($unserialized);
//expected output - unserialize: Array ( [0] => stdClass Object ( [color] => blue [type] => suv ) [1] => stdClass Object ( [color] => white [type] => mpv ) )


$decoded = json_decode($json);
print_r($decoded);
//expected output - decoded: Array ( [0] => stdClass Object ( [color] => blue [type] => suv ) [1] => stdClass Object ( [color] => white [type] => mpv ) )


$decoded2 = json_decode($json, true);
print_r($decoded2);
//expected output - decoded with true: Array ( [0] => Array ( [color] => blue [type] => suv ) [1] => Array ( [color] => white [type] => mpv ) )