What are multidimensional arrays? Just think of an array within an array. In other words, it's an array that stores arrays as elements. Those elements that are arrays themselves can have arrays as elements. The easiest way to explain this is with some examples. In the previous article on associative arrays, we looked at the $person array.
The $person array contains a few key/value pairs such as the person's name, age, email, occupation, and book_title. The book_title represents the book the author has written. I would assume that if a person makes it his or her profession being an author, he or she would eventually have more than one book. How would we store those books inside our $person array? We could create a new key for every new book.
But, we would have to modify the $person array each time the author writes a new book. Imagine now that a company that sells books wants to create an array for each author that they have listed. Some have 1 book published, others have hundreds. To guarantee that each author has a book inside the $person array, the company would have to provide each author with the max number of books. Let's say that the maximum number of books is 100. The author that wrote 100 books would have keys from book_title to book_title_100. The author that wrote 1 book would also have the same keys. If the author that wrote 100 books wrote an additional book, you would have to modify each author again. This seems like a lot of wasted time and space.
Luckily, we can store all of the books in an array, and then have one key, called books, point to that array.
In PHP, you can mix data types inside your array, meaning that we can store the array data type alongside the string, integer, boolean, etc, data types. In some programming languages, if you wanted to store an array as an element, each element inside the array would have to be an array. And not just an arbitrary array; it would have to be an array with the same number of elements.
In PHP, you can also store regular arrays inside associative arrays. If you look at our books array element, it only contains values. So how do you access an array element from inside the books array? You just keep appending brackets for each new dimension.
echo $person["books"][2]; // Prints: A Book on PHP
Since the books array is an array with no defined keys, we just use indices. Calling $person["books"] returns the entire books array. To access an element within that array, we append a couple of brackets to it and specify which array element we want to access.
Read the full article on my website
https://www.dinocajic.com/php-multidi...
Code for this tutorial
https://github.com/dinocajic/php-7-yo...
Full Code
https://github.com/dinocajic/php-7-yo...
PHP Playlist
• PHP Tutorial
--
Dino Cajic
Author and Head of IT
Homepage: https://www.dinocajic.com
GitHub: https://github.com/dinocajic
Medium: / dinocajic
Instagram: / think.dino
LinkedIn: / dinocajic
Twitter: / dino_cajic
My Books
An Illustrative Introduction to Algorithms
https://www.amazon.com/dp/1686863268
Laravel Excel: Using Laravel to Import Data
https://amzn.to/4925ylw
Code Along With Me - PHP: From Basic to Advanced PHP Techniques
https://amzn.to/3M6tlGN