PHP Multidimensional Arrays

PHP Multidimensional Arrays: How Many Dimensions Deep Have you Been?

What are PHP 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 of 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 while 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 you 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 of 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.

 

<?php
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.

Now that we understand the basics, let’s just dive head first into this and get over any kind of fear of multidimensional arrays that you might have. We’re going to go several levels deep. If you know how to go 2 dimensions deep, you should know how to go 50 dimensions deep. We’ll start by creating a simple $car associative array. Initially it will have the year, make, and model.

 

<?php$car = [
  'year'  => 2020,
  'make'  => 'Nissan',
  'model' => 'GTR'
];?>

 

Most car models have different trims, and the number of trims varies between car models and car brands. For this $car, we’re going to add another associative array as an element for trims.

 

 

Each trim points to another array. We’re going to be storing details about each trim, such as price and specs, inside the specific trim array.

 

 

The price is pretty easy. Each trim (premium, 50th_anniversary, and track_edition) has an associative array and the first element inside the associative array is the price. The price key points to a string value. There are multiple specs, such as horsepower and torque, that needs to be stored in yet another array. The specs key points to the specs array.

 

 

The horsepower (hp) and torque keys point to integer values, but fuel_economy points to an array since we can have highway, city, and even the type of gas required.

 

 

For the fuel economy associative array, we have the type, city, and highway keys pointing to either a string or floating point value. Alright, we built this massive multidimensional array. How do we access values now? Remember that I said to just keep appending brackets to the main array. So let’s see how we would access the price of the premium trim.

  1. Start with the $car array.
  2. Find where the premium trim resides. It looks like trims points to an array that contains the premium key.
  3. Append trims to $car: $car[‘trims’]. You now have access to the array that trims is pointing to.
  4. Append premium to $car[‘trims’]: $car[‘trims’][‘premium’]. You now have access to the array that premium is pointing to.
  5. The first element inside the premium array is the price. Append price to the end of your chain: $car[‘trims’][‘premium’][‘price’].

Of course, you can just echo that out since price is stored as a string.

 

<?php echo $car['trims']['premium']['price'];?>

 

What if we wanted to know the City MPG for the Track Edition model? Again follow the same approach and keep going into arrays.

$car[‘trims’][‘track_edition’][‘specs’][‘fuel_economy’][‘city’]

Car contains a key trims that points to an array. The trims array contains a key, track_edition, that points to an array. The track_edition array contains a key, specs, that points to an array. The specs array contains a key, fuel_economy, that points to an array. The fuel_economy array contains a key, city, that points to a floating point number.

Seems scary when you first see it, but as long as you strategically work your way deeper into the array, it’s relatively straight forward.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Associative Arrays

I’d Like you to Meet my associate.

PHP – P10: Associative Arrays

If you’re familiar with other programming languages, an associative array is pretty much a dictionary. Instead of accessing the array by using an index value, you’ll access it by using a key.

PHP Multidimensional Arrays

How Many Dimensions Deep Have you Been?

PHP – P11: Multidimensional Arrays

What are PHP 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.

PHP Variables

MORE MATH? X + 3 = 5 KIND OF VARIABLES?

PHP – P12: VARIABLES

If you’re familiar with other programming languages, an associative array is pretty much a dictionary. Instead of accessing the array by using an index value, you’ll access it by using a key.

Leave a Reply