How to Store PHP Arrays in a Database: 5 Practical Methods

This guide explains five ways to persist PHP arrays in a database—including serialize(), JSON encoding, normalized relational tables, comma‑separated strings, and Base64‑encoded data—detailing code examples, required field types, insertion steps, and retrieval techniques.

php Courses
php Courses
php Courses
How to Store PHP Arrays in a Database: 5 Practical Methods

1. Store with serialize()

PHP’s built‑in serialize() converts an array into a storable string while preserving keys and nested structures. Example:

$data = ['name' => '张三', 'age' => 28, 'hobbies' => ['阅读', '游泳']];
$serialized = serialize($data);

Insert $serialized into a TEXT column via PDO or MySQLi. Retrieve the string and restore the original array with unserialize():

$original_array = unserialize($fetched_string);

2. Store as JSON with json_encode()

JSON provides language‑agnostic readability. Encode the array:

$json_string = json_encode($data, JSON_UNESCAPED_UNICODE);

Save the resulting string in a TEXT or LONGTEXT column. When reading, decode back to an associative array:

$array = json_decode($fetched_json, true);

3. Normalize into a related table

When the array represents multiple entities (e.g., order items), store each element as a separate row in a child table linked by a foreign key.

INSERT INTO orders (user_id, created_at) VALUES (123, NOW()); -- get order_id
INSERT INTO order_items (order_id, product_name, quantity) VALUES (1001, '笔记本', 2);

Wrap the inserts in a transaction to ensure consistency, and retrieve combined data with JOIN queries.

4. Store as a comma‑separated string with implode()

Suitable for simple one‑dimensional indexed arrays of strings:

$tags = ['php', 'mysql', 'html'];
$tag_string = implode(',', $tags);

Save $tag_string in a VARCHAR or TEXT field. Retrieve and split back with explode():

$tags_array = explode(',', $fetched_string); // trim elements as needed

5. Store Base64‑encoded serialized data

When the field must avoid special characters or you need lightweight obfuscation, encode the serialized or JSON string with Base64.

$raw = serialize($data);
$encoded = base64_encode($raw);
// store $encoded
$restored = unserialize(base64_decode($fetched_encoded));

Decode with base64_decode() and then unserialize() (or json_decode() if JSON was used).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JSONmysqlPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.