Serverless, ReactPHP, and Expanding Frontiers, May 2019

(singke) #1
28 \ May 2019 \ http://www.phparch.com

Education Station


Data Structures, Part One

Chris Tankersley


I’ll admit, one of the best things I love about PHP is that I don’t have to deal with all sorts of
different ways to deal with data. At the base of the language we have nice primitives like strings,
integers, floats, and Booleans, and we also have one of the most flexible structures known to
programmers—the PHP Array.

The array itself wears many hats. If you are coming from other languages, you have to think about


the structure of ordered data in a few different ways. The reason for this tends to deal mostly
with how we need to represent data and how we manipulate data. Different data has different
requirements.

The PHP array spoils us. It alone can act like discreet data
types in other languages, and at times can act like multiple
data types. There is a ton of flexibility in this little data type,
but there are times when there are other options in PHP that
may be better suited.
For now, let’s dive into how we can use traditional data
structures in PHP, in their various forms.

Lists
Lists are the simplest way to work with arrays in PHP. A list
is simply an ordered blob of values. In PHP, we tend to call
this an enumerated array.

a = [1, 2, 3, 4] // Python
let a = [1, 2, 3, 4] // ECMAScript 6
$a = [1, 2, 3, 4] // PHP
a = {1, 2, 3, 4} // Lua


In PHP, when we do not specify keys for our array, we end
up with what other languages call a list. A list is keyed numer-
ically, starting at 0 (except for Lua, because it starts at 1 for
some awful reason.) You can normally iterate over a list, or
pull out a value from a specific key.

Note that in Lua, lists are called tables. Like PHP, Lua
uses a single data structure to represent a few different
things. In Golang, this same kind of structure is called a
slice, and lists behave differently than a slice. Just keep in
mind not every language has a list type.

PHP implicitly assigns numeric keys from 0 to N. We can
see this by doing a foreach loop over our array as in Listing 1.
Even though we did not specify a key, PHP assigned
numeric ones for us.
Lists are great when you are working on a set of values but
do not care about specific values. For example, if you are iter-
ating over a set of values, lists are a great type to use. If you
need to deal with things like a queue, lists work reasonably
well for this.

With lists, you are generally more concerned with just
getting values from the list in a specific order, like first to last,
then pulling out a specific value, as the third one.
While we can use the PHP array as a basic list, the language
also provides a few other built-in classes for working with
specific types of lists. We will look at those in a moment.

Dictionaries
The other way a PHP array can be used is much like a
dictionary. In PHP, you may see this referred to as an asso-
ciative array. The main difference between this data structure
and a list is that the values have a specific key which can be
used for retrieval.
In a dictionary, values are added to the structure with
specific names called keys. This data structure is used when
you need to store a specific value and retrieve a specific value
later.

a = {"key": "value", "key2": "value2"} // Python
var a = {"key": "value", "key2": "value2"} // Javascript
// ECMAScript 6
let a = new Map([["key", "value"], ["key2", "value2"]])
$a = ["key" => "value", "key2" => "value2"]; // PHP
a = {["key"] = "value", ["key2"] = "value2"} // Lua
// golang
a := map[string]string{"key": "value", "key2": "value2"};

Listing 1


  1. $a = [ 1 , 2 , 3 , 4 ];

  2. foreach($a as $key => $value) {

  3. print $key. PHP_EOL;

  4. }

  5. // Output

  6. // 0

  7. // 1

  8. // 2

  9. // 3

Free download pdf