Introduction: The Eternal Dilemma of a PHP Developer
\n\nEvery 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.
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\nWhat is Serialization in PHP?
\n\nSerialization 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\nPHP 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
It would seem that both do the same thing. But the devil, as always, is in the details.
\n\nExample 1: Simple Associative Array
\n\nLet'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\nEven 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.
Example 2: Multidimensional Array
\n\nLet'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\nConclusion: JSON is usually more compact, especially for large arrays. This is critical when storing in a database or transmitting over a network.
\n\nPerformance: Which is Faster?
\n\nSpeed 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\nResults (on PHP 8.2):
\n- \n
- Serialize: ~1.8 sec \n
- JSON:
Blogs
Book Recommendations
Read also
Why Java Executes Code in Comments via Unicode: Analysis
JSX.Element, ReactNode, ReactElement: When to Use What in TypeScript?
Metaprogramming in Swift: The Magic of Code That Writes Code
Dependency Injection in Kotlin in Practice: A Complete Guide
Monorepo with PHP: Code Organization and Best Practices