JSON or Serialize: What to Choose for Storing PHP Arrays? (Complete Guide)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Eternal Dilemma of a PHP Developer

\n\n

Every PHP developer has at least once faced the task: "How to save an array to a file, database, or cache?" On Stack Overflow, the question "Preferred method to store PHP arrays (json_encode vs serialize)" has gathered thousands of views and dozens of answers. And this is not surprising, because the choice between json_encode() and serialize() affects the performance, compatibility, and even security of your code.

\n\n

In this article, we will analyze both methods using real examples, compare their strengths and weaknesses, and give clear recommendations on when to use which. Let's go!

\n\n

What is Serialization in PHP?

\n\n

Serialization is the process of converting a complex data structure (array, object) into a string that can be saved or transmitted. The reverse process is called deserialization.

\n\n

PHP offers two main tools for this task:

\n\n
    \n
  • serialize() / unserialize() — PHP's built-in mechanism.
  • \n
  • json_encode() / json_decode() — working with the JSON format.
  • \n
\n\n

It would seem that both do the same thing. But the devil, as always, is in the details.

\n\n

Example 1: Simple Associative Array

\n\n

Let's start with the simplest case — an associative array with user data:

\n\n
<?php\n$user = [\n    'name' => 'Alexey',\n    'email' => 'alex@example.com',\n    'age' => 30,\n    'is_admin' => false\n];\n\n// Serialization\n$serialized = serialize($user);\necho $serialized . PHP_EOL;\n// Output: a:4:{s:4:"name";s:14:"Alexey";s:5:"email";s:17:"alex@example.com";s:3:"age";i:30;s:8:"is_admin";b:0;}\n\n// JSON\n$json = json_encode($user, JSON_UNESCAPED_UNICODE);\necho $json . PHP_EOL;\n// Output: {"name":"Alexey","email":"alex@example.com","age":30,"is_admin":false}\n?>
\n\n

Even at this stage, the difference is visible: serialize() produces a "dirtier" string with data types (s: — string, i: — integer, b: — boolean). JSON, on the other hand, looks clean and readable.

\n\n

Example 2: Multidimensional Array

\n\n

Let's complicate the task and save an array with nested data — for example, a list of items in a shopping cart:

\n\n
<?php\n$cart = [\n    ['id' => 101, 'name' => 'Book "PHP for Pros"', 'price' => 1500, 'quantity' => 2],\n    ['id' => 102, 'name' => 'Course "JSON Master"', 'price' => 3000, 'quantity' => 1],\n    ['id' => 103, 'name' => 'Certificate', 'price' => 500, 'quantity' => 3]\n];\n\n// Compare string sizes\n$serialized = serialize($cart);\n$json = json_encode($cart, JSON_UNESCAPED_UNICODE);\n\necho "Serialize: " . strlen($serialized) . " bytes\\n";\necho "JSON: " . strlen($json) . " bytes\\n";\n// Output (approximate):\n// Serialize: 218 bytes\n// JSON: 188 bytes\n?>
\n\n

Conclusion: JSON is usually more compact, especially for large arrays. This is critical when storing in a database or transmitting over a network.

\n\n

Performance: Which is Faster?

\n\n

Speed is one of the key factors. Let's test it on 10,000 iterations:

\n\n
<?php\n$bigArray = [];\nfor ($i = 0; $i < 1000; $i++) {\n    $bigArray[] = [\n        'id' => $i,\n        'name' => 'Element #' . $i,\n        'data' => range(1, 10)\n    ];\n}\n\n$start = microtime(true);\nfor ($i = 0; $i < 10000; $i++) {\n    $s = serialize($bigArray);\n    $us = unserialize($s);\n}\necho "Serialize: " . (microtime(true) - $start) . " sec\\n";\n\n$start = microtime(true);\nfor ($i = 0; $i < 10000; $i++) {\n    $j = json_encode($bigArray);\n    $uj = json_decode($j, true);\n}\necho "JSON: " . (microtime(true) - $start) . " sec\\n";\n?>
\n\n

Results (on PHP 8.2):

\n