I used to think $_SESSION was just for strings and integers. I was wrong. You can store entire arrays, nested structures, the works. PHP handles the serialization for you in the background. You don’t need to call serialize() or json_encode(). It just works.
Here is the direct answer if you are looking for it right now:
<?php
session_start();
$_SESSION['cart'] = ['apple', 'banana'];
?>
That is it. You assigned an array to a session key. Now let’s look at how to actually use this without breaking your app.
Every page needs session_start()
This is the most common mistake I see. People assign data in one file and try to read it in another, and the data is gone.
The reason is simple. session_start() initializes the session context for the current request. If you forget it on a page that reads or writes $_SESSION, PHP doesn’t know which session file to open. You get an empty array or a notice.
So, put session_start() at the very top of every script that touches the session. Before any HTML output. If you have a shared header file, put it there. But make sure it runs before you try to access $_SESSION.
I learned this the hard way when my login state vanished after adding a new page. I had forgotten the start call.
Adding and updating items
Once you have an array in the session, treat it like any other array. You can append items, update values, and delete them.
To add a new item to a list, use the [] syntax:
$_SESSION['cart'][] = 'orange';
If you are building a cart with specific products, associative arrays are much cleaner. You can store details like name and quantity under a product ID.
$productId = 101;
$_SESSION['cart'][$productId] = [
'name' => 'Keyboard',
'qty' => 2
];
To update the quantity later, just access that nested key:
$_SESSION['cart'][$productId]['qty']++;
This is where it gets powerful. You are storing structured data in a temporary variable that persists across requests. No database needed for simple state like this.
Reading and iterating
When you need to display the items, loop through them. A foreach loop works exactly as expected because PHP deserializes the array back into memory when the session starts.
$cart = $_SESSION['cart'] ?? [];
foreach ($cart as $id => $item) {
echo "{$item['name']} ({$item['qty']})<br>";
}
Notice the ?? [] part. That is important. If the user has never added anything to their cart, $_SESSION['cart'] might not exist. Accessing it directly throws a warning. Using the null coalescing operator gives you a safe empty array to work with.
I always guard my session reads this way, checking if the value exists before I touch it. It saves me from annoying notices in my logs.
Removing items and reindexing
Sometimes you need to remove an item. Use unset():
unset($_SESSION['cart'][$productId]);
There is a catch with lists. If you have a numeric array like [0 => 'apple', 1 => 'banana', 2 => 'orange'] and you delete index 1, you are left with [0 => 'apple', 2 => 'orange']. The keys are no longer sequential.
If your code relies on sequential indexing, or if you want to clean up the array, use array_values():
$_SESSION['cart'] = array_values($_SESSION['cart']);
This reindexes the array to start at 0. It is a small step, but it prevents subtle bugs later when you try to access items by position.
What not to store
I have made this mistake too. I tried to store entire database result sets in the session. Big arrays with hundreds of rows.
It works until it doesn’t.
Here is why: PHP reads the entire session file into memory at the start of the request. It writes the entire file back at the end. And while your script runs, the session file is locked. If you store a 50KB array, you are reading and writing 50KB on every single page load.
Worse, if two requests happen for the same user at the same time, one waits for the other because of the lock. Your app feels slow.
Keep it small. Store IDs, flags, and small objects. If you need heavy data, store the ID in the session and fetch the rest from the database or cache.
Just an array
You don’t need to do anything special to store arrays in PHP sessions. You assign them, you read them, you modify them. PHP handles the serialization and deserialization transparently.
The only rules that matter are: start the session on every page, guard against missing keys, and keep the payload small.
That is all there is to it.