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.
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:
php1<?php 2// Associative array with grades as keys and arrays of students as values 3$school_directory = array( 4 "Grade1" => array("Amy", "Bobby", "Charlie"), 5 "Grade2" => array("David", "Eve", "Frank"), 6 "Grade3" => array("George", "Hannah", "Ivy") 7); 8 9// Prints the Grade1 list in the associative array 10foreach ($school_directory["Grade1"] as $student) { 11 echo $student . " "; 12} // Output: Amy Bobby Charlie 13?>
Just like their non-nested versions, creating nested structures in PHP is straightforward.
Nested Associative Array:
php1<?php 2// Associative array within an associative array 3$nested_map = array( 4 "fruit" => array( 5 "apple" => "red", 6 "banana" => "yellow" 7 ), 8 "vegetable" => array( 9 "carrot" => "orange", 10 "spinach" => "green" 11 ) 12); 13 14// Prints the nested associative array 15foreach ($nested_map as $category => $items) { 16 echo $category . ": "; 17 foreach ($items as $item => $color) { 18 echo $item . "(" . $color . ") "; 19 } 20 echo "\n"; 21} 22?>
Nested Indexed Array:
php1<?php 2// Arrays within an array 3$nested_array = array( 4 array(1, 2, 3), 5 array(4, 5, 6), 6 array(7, 8, 9) 7); 8 9// Prints the nested array 10foreach ($nested_array as $arr) { 11 foreach ($arr as $val) { 12 echo $val . " "; 13 } 14 echo "\n"; 15} 16?>
Nested Associative and Indexed Arrays:
php1<?php 2// Arrays within an associative array 3$map_of_arrays = array( 4 "numbers" => array(1, 2, 3), 5 "more_numbers" => array(4, 5, 6) 6); 7 8// Prints the associative array with nested arrays 9foreach ($map_of_arrays as $key => $array) { 10 echo $key . ": "; 11 foreach ($array as $val) { 12 echo $val . " "; 13 } 14 echo "\n"; 15} 16?>
The retrieval of values from nested associative or indexed arrays follows rules similar to those for their non-nested counterparts.
From Nested Associative Array:
php1<?php 2// Associative array within an associative array 3$nested_map = array( 4 "fruit" => array( 5 "apple" => "red", 6 "banana" => "yellow" 7 ), 8 "vegetable" => array( 9 "carrot" => "orange", 10 "spinach" => "green" 11 ) 12); 13 14// Accessing apple's color from nested associative array 15echo $nested_map["fruit"]["apple"]; // Output: red 16?>
From Nested Indexed Array:
php1<?php 2// Arrays within an array 3$nested_array = array( 4 array(1, 2, 3), 5 array(4, 5, 6), 6 array(7, 8, 9) 7); 8 9// Accessing the 3rd value from the 2nd array in nested indexed array 10echo $nested_array[1][2]; // Output: 6 11?>
From Both:
php1<?php 2// Arrays within an associative array 3$map_of_arrays = array( 4 "numbers" => array(1, 2, 3), 5 "more_numbers" => array(4, 5, 6) 6); 7 8// Accessing the second value from the 'numbers' array in map_of_arrays 9echo $map_of_arrays["numbers"][1]; // Output: 2 10?>
The modification of nested arrays is similar to that of non-nested versions.
Operations on Nested Associative Arrays:
php1<?php 2// Associative array within an associative array 3$nested_map = array( 4 "fruit" => array( 5 "apple" => "red", 6 "banana" => "yellow" 7 ), 8 "vegetable" => array( 9 "carrot" => "orange", 10 "spinach" => "green" 11 ) 12); 13 14// Modifying spinach's color to red 15$nested_map["vegetable"]["spinach"] = "red"; 16 17// Adding cherry to the 'fruit' map in nested_map 18$nested_map["fruit"]["cherry"] = "red"; 19 20// Deleting apple from the 'fruit' map in nested_map 21unset($nested_map["fruit"]["apple"]); 22?>
Operations on Nested Indexed Arrays:
php1<?php 2// Arrays within an array 3$nested_array = array( 4 array(1, 2, 3), 5 array(4, 5, 6), 6 array(7, 8, 9) 7); 8 9// Adding 10 to the first array in nested array 10$nested_array[0][] = 10; 11 12// Deleting the 2nd value from the 3rd array in nested array 13unset($nested_array[2][1]); 14?>
Well done! You've successfully navigated through nested associative and indexed arrays, concepts increasingly valuable in handling complex data scenarios. We've explored how to create, access, and modify values in these intricate structures.
Next, we have practical exercises to consolidate your understanding of these concepts. Keep up the great work!