A nested array in PHP is just an array that contains other arrays. It’s how you model real-world data where things have sub-items.

Here’s the quickest way to create one:

<?php
$meal = [
    'dessert' => ['cake', 'cookies', 'sugar'],
    'bitter'  => ['pepper', 'spicy adana', 'mexican sauce']
];

echo 'I ate '. $meal['dessert'][0]; // I ate cake

You can nest them as deep as you need to. It’s not a special structure, just arrays inside arrays.


What is actually happening here

When you write $meal['dessert'][0], you are telling PHP two things in order. First, get the value at key 'dessert'. That value happens to be another array: ['cake', 'cookies', 'sugar']. Second, from that inner array, get index 0.

You don’t need a special function or syntax. The bracket notation just chains. $array['key'][0] is valid as long as the first part returns an array.

I used to think you needed some helper to pull out nested values. You don’t. The brackets do the work.


Building it step by step

Sometimes I build these arrays incrementally, adding one key at a time. This is cleaner for complex structures:

<?php
$meal = [];
$meal['dessert'] = ['cake', 'cookies', 'sugar'];
$meal['bitter']  = ['pepper', 'spicy adana', 'mexican sauce'];

// Add another item to an existing nested array
$meal['dessert'][] = 'baklava';

That [] syntax appends to the end of the inner array. $meal['dessert'][3] is now 'baklava'. No need to know the current length. PHP handles it.

I’ve seen people write loops to append items one by one with explicit indices. Don’t do that unless you have a reason. The [] operator is shorter and less error-prone.


Looping through nested data

When you need to visit every item in every sub-array, you use a nested foreach. It’s straightforward:

<?php
foreach ($meal as $category => $items) {
    foreach ($items as $item) {
        echo "$category: $item\n";
    }
}

Output looks like this:

dessert: cake
dessert: cookies
dessert: sugar
dessert: baklava
bitter: pepper
bitter: spicy adana
bitter: mexican sauce

I know this looks obvious. But I’ve watched people try to write a single loop with complex index math to flatten it. You don’t need that. Two loops are clearer and easier to debug.


Checking if a nested key exists

This trips me up more than anything else. If you try to access $meal['salty'][0] and 'salty' doesn’t exist, PHP throws a notice (or warning, depending on your version) and returns null. It’s annoying in production logs.

Use isset() to check before accessing:

<?php
if (isset($meal['dessert'][0])) {
    echo $meal['dessert'][0];
}

Or use the null coalescing operator for a default value:

<?php
echo $meal['salty'][0] ?? 'nothing';

That second one is my go-to. It’s readable, safe, and short. No nested if blocks needed.


Counting and inspecting

count($meal) only counts the top-level keys. In the example above, that’s 2 (dessert and bitter). It does not count the items inside those arrays.

If you need the total number of elements at every level, use the second parameter:

<?php
echo count($meal, COUNT_RECURSIVE); // 9

That counts the inner arrays themselves too. So it’s 2 categories plus 7 items. If you only want the items, subtract the top-level count: 9 - 2 = 7.

To actually look at the structure without guessing, dump it:

<?php
print_r($meal);
// or
echo json_encode($meal, JSON_PRETTY_PRINT);

I use json_encode with JSON_PRETTY_PRINT in development because it’s easier to read than var_dump for deep structures.


Where you actually see this

The most common real-world shape is database results. When you query a table and get multiple rows, PHP returns an array of associative arrays. Each row is an array, and the whole result set is an array of those.

<?php
// $users is an array of arrays
// $users[0] = ['id' => 1, 'email' => 'a@b.com']
// $users[1] = ['id' => 2, 'email' => 'c@d.com']

$emails = array_column($users, 'email');

array_column pulls one field from every row in the outer array. It’s built for this exact shape. You’ll use it constantly when working with database data.

If you’re still fuzzy on basic arrays, start here.


It’s just brackets

Nested arrays aren’t a special feature. They’re not a different data type. They’re just arrays that happen to hold other arrays. The syntax is the same, the logic is the same, and the tools you use are the same.

Once you stop thinking of it as something extra, it stops being confusing. You build it, access it with chained brackets, loop through it with nested foreach, and check keys with isset or ??. That’s the whole thing.