Data Structures in PHP

Reading Time: 2 minutes

Data Structures are a hallmark of every computer science program at the university level, and it is unthinkable that throughout the course of study you will never encounter a specific data structure that you are unfamiliar with. As I return to school to pursue a degree in computer science I decided it would be a fun and interesting challenge to implement common data structures in my “native” coding language – PHP.

I’d like to consider this an introduction to the series on my blog. What I plan to do is outline a few data structures I want to implement in PHP. So, without further adieu…

Linear Data Structures

Linear data structures are exactly what they sound like… data structures that arrange information sequentially, or: in a line. Linear data structures are the most familiar for people new to programming because we use them even without using code. A grocery list or a chore list is an easy real-world example of a linear data structure. We have the beginning of the list, the end of the list, and we perform actions like crossing items off the list or adding new ones to the bottom.

Naturally, this format has its benefits for real-world applications, and it is easy to visualize and conceptualize in code, so these data structures will be the first I implement in the series:

  • Singly Linked List
  • Doubly Linked List
  • Queue
  • Deque
  • Stack

Non-Linear Data Structures

Non-linear data structures are a bit more complex, and admittedly these are the data structures I struggled with most the last time I took a data structures and algorithms class in my first degree program. But now that I have a few years of development experience under my belt for PHP, and I am comfortable with web programming at an intermediate level, I feel confident I can implement these more complex structures in a language I am most familiar with.

As the name implies, non-linear data structures are just that, not linear. They’re not organized in a list, and they’re not necessarily sequential. A good example of a real world implementation of non-linear structures are hierarchical and graph structures. Think of your friend group. There’s your friends, your friends of friends, and your friends of friends’ mutual friends. If you were to draw the connections out on a piece of paper, the resulting sketch would look like a complex spider web of interactions. These data structures are harder to conceptualize, but offer unique perspectives and methods for interacting with the information stored within. Non-linear data structures will be the second half of the series, and in it I will focus on the following:

  • Binary Trees
  • Graph Structures

Ignored Data Structures

There are some default data structures that exist in many programming languages, and PHP is no exception. These data structures might include strings, lists, arrays, collections, etc. I will not be working on any of the data structures that PHP includes out-of-the-box, like Arrays. I will also not be covering advanced applications of these built-in structures like matrix arrays, multidimensional arrays and associative arrays (key-value arrays).

I hope this series is helpful to people interested in PHP or data structures, and I am excited to get started!