PHP arrays can nest arbitrarily deep, and multidimensional arrays are the default way to represent structured data (a database result set, a JSON response) before it’s mapped into objects.
$products = [
['name' => 'Widget', 'price' => 9.99, 'tags' => ['new', 'sale']],
['name' => 'Gadget', 'price' => 19.99, 'tags' => ['featured']],
];
foreach ($products as $product) {
echo $product['name'] . ': $' . $product['price'] . "\n";
foreach ($product['tags'] as $tag) {
echo " #$tag\n";
}
}
// Sorting by a nested key
usort($products, fn($a, $b) => $a['price'] <=> $b['price']);
// Flattening for display
$allTags = array_merge(...array_column($products, 'tags'));
For anything beyond two or three levels of nesting, consider whether the data actually belongs in proper objects instead — deeply nested arrays get hard to reason about, and typos in array keys fail silently rather than throwing an error.