Skip to content

PHP Tutorial Arrays

Multidimensional Arrays

Lesson 120 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

An array item can be another array. This lets you build tables and nested records.

A list of records

This is the most common shape: a list where each item is an associative array.

index.php
PHP
<?php
$products = [
    ["name" => "Notebook", "price" => 80, "stock" => 25],
    ["name" => "Pen", "price" => 12, "stock" => 100],
    ["name" => "Bag", "price" => 799, "stock" => 0],
];

echo $products[0]["name"] . "\n";
echo $products[2]["price"];
Output
OUTPUT
Notebook
799

Read it from left to right: $products[0] is the first product, and ["name"] is its name.

Loop through it

index.php
PHP
<?php
$products = [
    ["name" => "Notebook", "price" => 80],
    ["name" => "Pen", "price" => 12],
];

foreach ($products as $product) {
    echo $product["name"] . ": " . $product["price"] . "\n";
}
Output
OUTPUT
Notebook: 80
Pen: 12

A grid (rows and columns)

index.php
PHP
<?php
$grid = [
    [1, 2, 3],
    [4, 5, 6],
];

echo $grid[1][2] . "\n";   // row 1, column 2

foreach ($grid as $row) {
    echo implode(" ", $row) . "\n";
}
Output
OUTPUT
6
1 2 3
4 5 6

Nested records

index.php
PHP
<?php
$order = [
    "id" => 1042,
    "customer" => ["name" => "Ravi", "city" => "Surat"],
    "items" => [
        ["sku" => "A1", "qty" => 2],
        ["sku" => "B7", "qty" => 1],
    ],
];

echo $order["customer"]["city"] . "\n";
echo count($order["items"]) . " items\n";
echo $order["items"][1]["sku"];
Output
OUTPUT
Surat
2 items
B7

Change a nested value

index.php
PHP
<?php
$order = ["customer" => ["city" => "Surat"]];

$order["customer"]["city"] = "Pune";
$order["customer"]["phone"] = "99999";

print_r($order);
Output
OUTPUT
Array
(
    [customer] => Array
        (
            [city] => Pune
            [phone] => 99999
        )

)

Safe reading with ??

When a nested key might not exist, chain ??.

index.php
PHP
<?php
$order = ["customer" => ["name" => "Ravi"]];

echo $order["customer"]["email"] ?? "no email";
Output
OUTPUT
no email

Pull one column out

index.php
PHP
<?php
$products = [
    ["name" => "Notebook", "price" => 80],
    ["name" => "Pen", "price" => 12],
];

print_r(array_column($products, "price", "name"));
Output
OUTPUT
Array
(
    [Notebook] => 80
    [Pen] => 12
)

array_column() takes one field from every row. The optional third argument sets the keys.

info
This is how data really looks
Database results, JSON from an API and your own shopping cart all arrive as arrays of arrays. Getting comfortable with this shape pays off in every later chapter.