Jump to content
Larry Ullman's Book Forums

Confusion Over Multidimensional Array Foreach Tip

Recommended Posts

Okay so I 've created the books.php script like your book says and everything looks fine when viewing in my browser.  I decided to try the "access every element of every array" tip to see if it worked even though I am not understanding how the Title and Chapters are found when they haven't been declared.  What am I not understanding?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Larry Ullman's Books and Chapters</title>
<h1>Some of Larry Ullman's Books</h1>
<?php // Script 7.4 - books.php
/* This script creates and prints out a multidimensional array. */

// let me learn by my mistakes
ini_set ('display_errors', 1);
// show all possible problems
error_reporting (E_ALL | E_STRICT);

// Create the first array:
$phpvqs = array (1 => 'Getting Started with PHP', 'Variables', 'HTML Forms and PHP', 'Using Numbers');

// Create the second array:
$phpadv = array (1 => 'Advanced PHP Techniques', 'Developing Web Applications', 'Advanced Database Concepts', 'Security Techniques');

// Create the third array:
$phpmysql = array (1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to MySQL');

// Create the multidimensional array:
$books = array (
'PHP VQS' => $phpvqs,
'PHP Advanced VQP' => $phpadv,
'PHP and MySQL VQP' => $phpmysql

// Print out some values:
print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}</i>.</p>";
print "<p>The first chapter of my second book is <i>{$books['PHP Advanced VQP'][1]}</i>.</p>";
print "<p>The fourth chapter of my fourth book is <i>{$books['PHP and MySQL VQP'][4]}</i>.</p>";

// See what happens with foreach:
foreach ($books as $title => $chapters) {
	print "<p>$title";
foreach ($chapters as $number => $chapter) {print "<br />Chapter $number is $chapter";}
print '</p>';



Link to comment
Share on other sites

Sorry Larry, I knew the code was working just cannot understand how the foreach line is working when the $title and $chapters variables aren't assigned to anything.  Looks to me like the variables  $phpvqs, $phpadv and $phpmysql were assigned to the chapters and titles.  So I am having trouble understanding what makes the foreach work.

On 10/10/2020 at 2:07 PM, nootkan said:

foreach ($books as $title => $chapters) { print "<p>$title"; foreach ($chapters as $number => $chapter) {print "<br />Chapter $number is $chapter";} print '</p>';


Link to comment
Share on other sites

Ah, okay. So $phpvqs et al. represent their own indexed arrays, which means that indexing $phpvqs at 'PHP VQS' in the main array is the same as if you defined that sub-array there. 

Or put another way, imagine $phpvqs was equal to 2. Then $books['PHP VQS'] would also be 2. 

As for the actual variables $title and $chapters, those are named and created within the foreach loop to represent the index and the value at that index. These could be named anything--$k (or $key) and $v (or $value) are also commonly used. So the foreach loop doesn't "know" anything about the structure of the innermost arrays but as the programmer, we do and can use more descriptive variable names accordingly.

Let me know if anything is still unclear.  

Link to comment
Share on other sites


  • Create New...