Introduction

Welcome to our exploration of Nested Data Structures in PHP. After understanding the basics of PHP arrays, we'll delve into nested associative arrays and multidimensional arrays. These structures enable us to handle complex and hierarchical data, which is typical in real-world scenarios. This lesson will guide you through a recap of the basics, the creation and modification of nested arrays, as well as common operations.

Recap: Associative Arrays, Indexed Arrays, and Understanding Nested Structures

As a quick recap, PHP associative arrays store key-value pairs, and indexed arrays store elements in a linear fashion with numeric indices. These structures can be nested. Here's a simple example of a school directory using associative arrays:

PHP
<?php
// Associative array with grades as keys and arrays of students as values
$school_directory = array(
    "Grade1" => array("Amy", "Bobby", "Charlie"),
    "Grade2" => array("David", "Eve", "Frank"),
    "Grade3" => array("George", "Hannah", "Ivy")
);

// Prints the Grade1 list in the associative array
foreach ($school_directory["Grade1"] as $student) {
    echo $student . " "; 
} // Output: Amy Bobby Charlie 
?>
Creating Nested Associative and Indexed Arrays
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal