Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (5)

0 answers

Post by Shaleha Khanam on October 16, 2013

Yes, it is a great explanation about arrays. Many thanks Mathew.

0 answers

Post by Steven Morrison on October 15, 2013

Great explanation, thanks!

0 answers

Post by Justin Schlabach on March 28, 2013

I feel like I am watching John Madden sometimes when I watch these! haha

0 answers

Post by sandy lin on December 26, 2012

the homework exercises display the wrong slides!!

admin please update!

thanks.

0 answers

Post by Elizabeth Etta on October 19, 2012

THanks for the explanation on array

Arrays

  • An array is a composite data type meaning it can hold more than one piece of data simultaneously, unlike scalar data types.
  • Arrays in PHP are treated as maps where values are associated, or mapped, to keys .
  • Keys can only be either integers or strings. However, values can be any PHP data type, including other arrays.
  • Array values can be repeated, but keys must be unique.
  • Arrays with integers as keys are known as numerically-indexed, or simply, indexed arrays .
  • Arrays with strings as keys are known as associative arrays .
  • Arrays are typically declared using the array() construct.
  • Associative arrays use the => operator to assign keys with values.
  • Data in arrays is accessed using square bracket syntax .
  • For indexed arrays, the integer key of the desired value is listed between the brackets. Indexed array keys always start with the index 0.
  • For associative arrays, the string key of the desired value is listed in quotes between the brackets.
  • print_r() is a function used to output the value of variables in a formatted fashion.
  • Additional Resources:

Arrays

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:09
    • Lesson Overview
  • What is an Array? 0:42
    • What is an Array?
    • Arrays in PHP
    • Keys and Values
  • Types of Arrays 3:37
    • Indexed Arrays & Associative Arrays
  • array() Construct 6:47
    • Declaring Arrays
    • Defining Indexed Array
    • Defining Associative Arrays
  • Square Bracket Syntax 8:50
    • Square Bracket Syntax
    • Accessing Indexed Arrays
    • Accessing Associative Arrays
  • Arrays Example 10:41
    • Indexed Arrays Example
    • Associative Arrays Example
  • Multi-Dimensional Arrays 18:28
    • Multi-Dimensional Arrays
    • Multi-Dimensional Arrays Example
    • Multi-Dimensional Arrays in PHP File
  • Coding Conventions: Arrays 27:59
    • Coding Conventions: Arrays
  • Homework Challenge #1 29:20
    • Homework Challenge #1
  • Homework Challenge #2 30:38
    • Homework Challenge #2

Transcription: Arrays

Hello, and welcome back to Educator.com's Introduction to PHP course.0000

In today's lesson, we are going to be talking about what are known as arrays.0004

Specifically, we are going to be going over what actually an array is and the different types of arrays that are available in PHP.0010

We are going to talk about what is known as the array construct, which is how you declare arrays within your PHP code,0019

and the square bracket syntax, which is how you access values in an array.0025

We are going to talk about somewhat of an advanced topic called multidimensional arrays, 0032

and then, as usual, we are going to talk about coding conventions as they relate to arrays.0035

What is an array? Well, array is one of the 8 pre-defined data types in PHP, and it is known as a composite data type.0043

What that means is: it is a data type that can hold multiple data values at the same time.0051

This is opposed to scalar data types--and we have already learned about three of those: integer, float, and string.0056

And an integer, a float, and a string data type can only refer to one piece of data.0063

So, an integer can refer to one number; a float can refer to one floating point; a string can refer to one string.0067

Array, on the other hand, can be used to hold multiple integers, multiple floats, and multiple strings.0073

And actually, an array can hold any valid PHP type as a data value.0081

You actually can have an array that contains both integers, floats, strings...and in fact, because an array is a valid PHP data type,0091

it can actually hold an array as well, which would be known as a multidimensional array.0100

In PHP, arrays are treated as, essentially, maps.0105

Because an array represents multiple pieces of data, you have to have a way to access those individual pieces of data.0109

And so, what it does is: it creates a set of keys, and it associates different values with each key.0116

So, if you have an array with multiple numbers in it--let's say the temperature of the month--0123

you need a way to access each of those temperatures, and the way you do that is using the key.0129

In PHP, you can have two kinds of keys; they can be either integers or strings, and the values, as mentioned, can be any data type.0136

Values can be repeated, but the keys must be unique, because if you had a key that was two keys, 0149

how would you know which piece of data it refers to?0155

So, essentially, let's say we create an array here; we are going to call it (make this a little easier to read) arr.0157

We are going to define a variable that is an array data type.0171

Maybe you want it to hold five different numbers; let's say it holds 1, 2, 3, 4, and 5.0174

Now, the way that you access these different numbers, as mentioned, is through the use of a key.0183

So, we are going to associate a key with each of these different values.0187

For example, in what is known as a numerically indexed array, the number 0 is going to be used 0195

to refer to the first piece of data in array--in this case, the number 1; the number 1 will refer to the second piece; and so forth.0201

And the other option is that these keys could also be strings; and that is known as an associative array, which we're going to talk about now.0209

Arrays with integer keys, as we just showed, are known as numerically indexed, or just simply indexed arrays.0219

And arrays with string keys are known as associative arrays; so we have associative arrays and indexed arrays.0227

We had just looked at an example of an indexed array where you have...let's do some different numbers...a key associated with them of 0, 1, and 2.0236

The reason we use 0 is that indexed arrays always begin with the index 0.0252

So, if you define an indexed array, the way you access the first element or piece of data that you have added to that array is using the key 0.0257

We are going to learn about the syntax in a second, about how to do that.0268

The other option would be that you could define an associative array. 0272

And let's say we had the same data values, except that now we are going to use keys that are strings.0279

Actually, let's make this 50.0302

So now, in order to access this first piece of data, 36 (let's say these were temperatures during particular days of the week),0310

we would access this using the key day1; so the string day1 refers to the piece of data 36, day2 refers to 72, and day3 would refer to 50.0319

This is opposed to using the numbers 0, 1, and 2.0332

And so, associative arrays are quite useful, because they are a little more intuitive in your code,0336

and when you access elements, you get to use a description as to what piece of data you are getting from the array.0341

Here, we know that we are getting, for example, the temperature of day 1.0349

Up here, we used 0 to index the array, to access the value in the array.0353

0 doesn't have as much meaning, because every indexed array has a 0 in it.0359

One thing to also note is that, in PHP, arrays can be a mixture of indexed arrays and associative arrays,0366

meaning that they can have numeric indexes and string indexes, all in the same array.0376

So, a little more complicated, but not used quite as often...and so, we are not going to discuss them here, 0380

but in the Quick Notes, we have added an additional resources link that goes to the PHP manual web page for arrays 0386

that is going to discuss a little more about using this mixed type of array, if you are interested in that.0399

So, how do we define an array? Well, in PHP, it is defined using what is known as the array construct, 0409

which is the word array and then a pair of parentheses.0416

For indexed arrays, an array declaration is going to look like this: you have a variable name; you have the construct array0421

with its two parentheses; and then, you have a comma-delimited list of values in the array.0432

So, we have the number 95, the floating-point value 98.6, and the string Hello.0439

Now, because this is an indexed array, it uses integers as its keys.0446

Because indexed arrays in PHP begin with an index of 0, this is going to be referenced with the key 0,0450

this is going to be referenced with the key 2, and this is going to be referenced with the key 2.0459

Now, to define associative arrays, we use a similar syntax.0464

We have the variable name; we have the array construct with the parentheses; and we have, again, a comma-delimited list of values.0468

But instead of just values, they are actually key-value pairs.0478

So, we have the same data: 95, 98.6, and hello--the same values--and instead of using the default numeric keys that get associated with it,0481

we are actually naming the key we want it to associate with each piece of data.0491

So, for example, we are using the string grade to refer to 95; maybe that represents a grade on a test.0496

We are going to use the string temp to refer to the temp 98.6,0503

And maybe we have a string that is going to be a greeting, and so we would reference that with greeting.0507

It is important to note that the commas in these lists are very important, 0514

because it lets PHP know when you have finished defining one key-value pair in an array, versus another.0521

We know what arrays are; we know how to define them now; how do you access information in an array?0531

Well, the key is what is known as square bracket syntax; and it looks like this.0536

For example, let's say we want to access the second element of the array defined on the previous slide called indexedArray.0545

And it was actually...the value, if you remember, was 98.6; it was a temperature value.0554

Because it is the second element in that array, its key is 1.0560

And so, the way we access the element is: we have the array's name, 0566

followed by a pair of square brackets with the integer, not in quotes, in between the brackets.0572

And so, what this is going to do, if we echo this statement right here: it is going to output 98.6, 0579

because 98.6 is the value associated with the key number 1 in the indexed array.0585

Now, associative arrays use a similar syntax; you access elements by giving the name of the variable, followed by a pair of opening/closing square brackets,0597

and then you include the string, in the middle, which is the key for the piece of data that you want to access.0610

Now, the key here is that you need to enclose the string in single or double quotes.0618

Up here, we had the number, and it wasn't enclosed in quotes, because that way, it lets PHP know that you are using a numerically indexed array.0625

Down here (fix it so it's a little clearer), you need to enclose your key in quotation marks.0634

Let's take a look at some of these concepts in action in real files.0642

We have a file here called indexedArrays.php, and it is going to demonstrate some of the concepts related to indexed arrays.0647

And so, what we have done is: we have created an array called dailyTemp, and it is going to hold seven values,0655

and we are going to use it to represent the high temperature for the seven days of any particular week.0661

We have the variable name, and then, using the assignment operator, we assign it an array of values, and we use the array construct.0667

And then, we have seven different values, or seven different integers, in this case, which the array is going to be comprised of.0678

And you will notice, they are all separated by commas.0685

And then, we have a couple of statements that output every single one of the values in the array.0689

That is to demonstrate the use of the square bracket syntax.0694

If you remember, the first element in numerically indexed arrays in PHP is always 0.0701

So, to output the first element, 75, we would say dailyTemp, square bracket, the integer 0, and then closing square bracket.0706

And we do that so forth to access each of these individual elements in a row.0715

Now, if you notice, the seventh day (which is 65 degrees) actually has the index 6.0721

So, because the array indexes begin with 0, the first element is always going to start with 0, 0728

and the last element is always going to have the number that is one less than the number of items in your array.0734

And then, down here, we have a var_dump comment, which we have used before to output the type and value of scalar variables.0741

It can also be used for array variables; so if we go ahead and look at this script in our browser,0753

we can see that, using the square bracket syntax, we were able to output each individual day's temperature.0764

And then, also, we see the output of the var_dump function.0775

The way that works is: if it is given an array--if you pass an array to the var_dump function--it lets you know that you have an array variable.0779

7 in parentheses tells you how many elements, or how many pieces of data, are in the array.0790

And then, it goes through and lists the key-value pairs for the array.0795

So, since this is a numerically-indexed array, all of the keys are going to be numbers; so we have 0, 1, 2, 3, and so forth.0800

And then, it shows the value after this equals sign, greater than operator.0807

And it says that the key 0, or the first element in this array, is an integer with a value of 75.0812

And if you look here, we can see that they all correspond; so the second element, 76, with the index of 1--0819

it says down here that it's an integer with a value of 76.0826

So now, let's look also at associative arrays.0836

Here, we have created a similar array in a different file; it's a file called associativeArrays.php.0842

We have named the array variable dailyTemp again.0848

And this time, we are creating an associative array.0851

We have the same temperature values for each of the seven days, except that now, instead of numerically indexing the array0853

and using integers as the key, we are assigning a string to each value.0861

So, I made 7 keys, one for each day of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday),0866

and using this equal sign, greater than operator, assigned it the temperature value.0873

You notice the difference between the indexed array and the associative array.0884

In the indexed array, you simply list the values separated by commas; but in the associative array, you list key-value pairs.0889

They make use of the equal sign, greater than operator, which assigns the value to the right to the key on the left.0898

So, using square bracket syntax, we are going to output all of the different values in this array.0906

So, we have the variable name, followed by the opening square bracket.0912

Then, in quotations, we have the key name of the temperature that we want to output.0915

In this case, we want to output the temperature for Sunday, which is going to be 75.0922

And so, we have done that for each of the different days; 0927

and then again, we are going to have a var_dump function that is going to output the associative array, 0932

and we are going to take a look at what var_dump looks like for an associative array.0937

So, let's load the file up; and what you can see is: it has output all of the temperature values of the array.0941

Our square bracket syntax with the key enclosed in single quotes or double quotes worked.0954

And then, we have the output from var_dump, which shows that the array has 7 items; and then, it goes on and lists the key-value pairs.0961

Now, in this case, because it's an associative array, the keys are strings.0969

Instead of Sunday being 0 and Monday being 1, it actually lists the name of the key that we assigned when we defined the array.0973

Sunday is associated with an integer value that is 75; Wednesday is associated with an integer value that is 73.0982

That is how you use square bracket syntax to access associative array data.0992

And so, we have demonstrated that for associative arrays, as well as indexed arrays.0998

The other thing that you can use the square bracket syntax for is not only accessing the data, but you can use it to set data, as well.1003

For example, let's say...we'll go back to the indexedArrays.php example: we set up our daily temperature array with these seven values.1010

Let's say, "Oh, you know what, we made an error on Monday, or the first day; the temperature wasn't actually 75--it was 78 degrees."1018

What we can do is: we can use square bracket syntax to update that value in the array.1027

We use an assignment operator to do that, just like assigning an integer literal to an integer variable or a float literal to a float variable.1041

I'll put this at 78.1051

What this statement does is: this is going to say, "I want the data value in the dailyTemp array that is associated with the key 0 to now be 78."1053

So now, this array actually contains the numbers 78, 76, 73, 73, 80, 70, 65, and this first one is replaced.1069

If we go back and we view this code again, we are going to see that the first value has been updated from 75 to 78.1077

And as you can see, it has been done there.1091

So, square bracket syntax is used to access and get the values in a particular way, and it can also be used to update the values in a particular way.1093

As mentioned, arrays in PHP--their keys can only be integers or strings, but their values can be any valid PHP data type.1110

And an array is a valid PHP data type, so arrays can contain values that are, in themselves, arrays.1119

And when you have that situation occur, it is known as a multidimensional array.1127

We have an example here: let's say we want to create an array of addresses, 1132

an array that represents the city and state of the address of our two friends Joe and Jan.1139

What we do is: we create our first array, and we have two keys: we have a key joe, and we have the key jan.1147

And then, we assign them the values in the equal sign, greater than operator.1157

Instead of assigning them a scalar value (like an integer or a float or a string), we are going to assign them a composite value, or an array.1162

We are going to assign them an array that is an associative array, that has two pieces of data in it.1173

One is the city, and one is the state, in which that person lives, and we have named the keys city and state to refer to these.1181

You can see that, when we set joe equal to an array, we use the same array construct with the opening and closing parentheses.1192

And because we have now defined this key-value pair for Joe, we have a comma.1204

The comma says, "OK, now, in this original array that we declared, we are going to be defining a new key-value pair."1210

We come down here, and we are defining a key jan, and we are associating with it a data value that is also an array.1216

It's an associative array with two elements with the keys city and state, and I have the values, in this case, San Diego and California.1225

Let's take a look at multidimensional arrays in an actual PHP file.1235

Here, I have created the same multidimensional array in our PHP file; we have the addresses array, 1240

which is an array containing two key-value pairs, or two elements.1249

One has the key joe; one has the key jan; and each of these keys has a value associated with it,1255

which is an associative array containing a city and state element.1263

And so, the question, then, is: how do you access the elements in a multidimensional array?1270

We know, for square bracket syntax, that to access the elements in a regular array, 1275

you simply use a square bracket with an integer or a string to access the particular value.1280

Well, here we actually have two arrays; so, if we want to access, for example, the city that Joe lives in,1285

the way you do it is: you use a double square bracket syntax.1293

What you do is: you have the array name, and you use the first square bracket to describe the first data value that you want to get.1298

In this case, we want to load the array of Joe's city and state.1311

So, what we do is: we have the key joe in single quotes; and then, what this does is: this statement, alone, by itself, says,1317

"OK, give me the value associated with joe in the addresses array," and that is going to return this right here--this array.1325

Then, with this bracket, we are basically saying, "Within this array that we are now working with, 1334

we want to access the city key and access the value associated with it."1342

What this is going to do is: this is going to go into this array (which is two-dimensioned, because there is one array within another),1349

and it is going to say, "Look up the joe array, and then give me the name of the city in which Joe lives."1356

I have done that for both Joe and Jan, and I have shown down here--1365

we have a var_dump again, and that can be used on multidimensional arrays, as well.1371

I have included that, so when we look at this file, you can see how that output looks for a multidimensional array.1377

And also, I have introduced a new function that looks just like var_dump: 1382

you simply pass it the name of the variable that you want to output information about.1387

This one is called print_r, and it is another function that is built into PHP 1393

that allows you to get some information or output about some different variables, and it's useful for debugging.1398

If we go and look at this script in our browser, we can see: Joe lives in Norfolk.1404

And if we look back here, we have an echo statement, "Joe lives in," 1414

and then we are echoing the value that is associated with the city key in the array associated with the joe key in the addresses array.1418

It gets kind of complicated; and we can see that it did the same thing for jan.1433

Now, this is the output of var_dump; and we can see that, because in our original array there are only two key-value pairs--1439

there is joe and jan, and they are each associated with their own array--it says the length of the array addresses is 2.1448

Now, when it goes in here and it lists the key joe and it lists the value of joe, the value associated with key joe is actually also an array of length 2.1455

And the contents of that array are two key-value pairs.1467

One is a city that has a string of length 7 associated with the value Norfolk,1472

and one has a key called state with a string that is 2 characters long, called VA.1479

And it does the same thing for jan, as well; that is what the output of var_dump looks like.1487

The print_r output is somewhat similar, except it doesn't give information about the types of the data.1492

For example, here it says string, and here it says it's an array.1499

And it doesn't also give the lengths of the array or the lengths of the strings--stuff like that.1503

It just says we have an array; it has a key, joe, which is associated with another array; it has these key-value pairs;1509

and it has another key, jan, which is associated with another array with key-value pairs.1519

Now, this is known as a two-dimensional array; but you can go as deep as you want in this.1528

So, for example, let's say, instead of just Joe's city that he lives in right now in Norfolk, we want to be able to access the city he was born in, as well.1535

What we could do here is: for the city value (move this down here), instead of just associating it with Norfolk, we can associate another array.1546

This one is going to be a numerically indexed array, and the first value in the array is going to be where Joe currently lives,1559

and the second value is going to be where Joe was born.1568

Let's make it look at little neater here.1582

What we can see is: we have an array that contains two keys, and with them, each has an array associated with them.1584

This first array, joe, has two key-value pairs; one is city, which has another array associated as its value,1592

which contains two strings, and then it has a state for VA.1601

And so, the way that you access that information is similar to the square bracket syntax before, except you add another set of square brackets.1605

So, joe is going to refer to this value in the address's array; and once we get that, city is going to refer to this array.1613

And then, within this array, let's say we want to access the place that he was born--because it's an indexed array,1625

and the second element we defined as the place he was born, it is going to have the index 1.1634

We put 1 in here; and now, what that is going to do is access this element in this array, which is in this array, which is in this array.1640

This could go on forever and ever; so, what happens is: now, we should see the output that says "Joe lives in Richmond."1649

Let's load that up in our browser; and we can see, it's updated to show Joe living in Richmond, and var_dump also has updated, as well.1658

If you look through it carefully, you can see how it goes down three array levels deep.1668

Coding conventions: we are going to revisit this topic over and over, and in this case, we are going to talk about arrays.1680

In this course, for arrays declared using the array construct (which is a typical way of declaring them),1688

we are going to have them be formatted according to the Zend Framework's coding conventions for arrays.1695

Now, the Zend Framework is a PHP framework that is used to facilitate the building of PHP websites.1702

It is a very commonly-used framework, and if you stay in the PHP world, you might eventually become a developer using the Zend Framework.1709

But essentially, what they have done is developed coding conventions that all of their programmers that code for them need to follow.1718

We are going to follow those conventions for this course: and you can access the conventions at this website address here.1725

It is actually loaded up in the browser here.1733

This is the page it goes to: it goes to the Zend Framework Programmer's Reference Guide, and it talks about their coding style.1737

If you scroll down the page, they have a discussion on numerically indexed arrays and associative arrays,1743

and how you are supposed to format them, as far as spacing and tabs and so forth go.1752

That is the format we are going to use in this course.1756

For the homework challenge, we are going to have you mimic some of the things that we did in the lesson today.1763

Basically, I am going to have you create an indexed array and an associative array.1770

Your indexed array will be called itemsPurchased, and it is simply going to be an array containing five item ID's.1775

This array could maybe represent 5 items that a user has purchased on your website.1782

We are going to have the ID's be strings instead of numbers.1787

And then, we are also going to declare an associative array called months.1792

And in this case, all the keys of the array are going to be the strings 1, 2, 3, 4, all the way through 121796

And with each numeric key, we are going to associate using the equals, greater than operator, the appropriate month.1802

And then, I just want you to go ahead and output, as we had done in the in-class example, each value of itemsPurchased and months,1810

using individual echo statements, which will give you practice using the square bracket syntax.1818

Be sure to remember that indexed arrays start with the index 0.1824

And then, you can also output them using var_dump to get practice at seeing what the output of var_dump looks like for arrays.1828

For the second homework challenge, it is a little more difficult.1840

I'm going to have you create a multidimensional array called monthlyTemps, and it is going to be an associative array.1842

The keys for the array are going to be the names for the months (January, February, March, April, and so forth).1851

And with each name--with each key--you are going to associate an associative array that has two elements--1856

an element with a key called high and an element with a key called low.1867

To each of those, you are going to assign an integer value that represents the high and low temperature for that particular month.1877

You are going to have the first array--the keys will be months--and then, with each of those keys, you associate another array1884

that is an associative array that has the high and low temperature for the month.1892

And then, I want you to practice using the square bracket syntax, which again, is going to involve double square brackets.1898

And because they are both associative arrays, we use quotes around the indexes or keys for the array.1908

This is going to give you practice doing that.1920

I just want you to go through and output individually each of the high and low temps for the different months.1921

You don't have to do it for all of them, but just do it for a couple, to get a feel...practice for using multidimensional arrays.1927

And then, be sure to try and use var_dump and print_r, the new function we learned about, 1932

to output the structure and values in the array that you define.1939

That will give you help in understanding the structure of the array, and it will also help you understand the output that var_dump and print_r generate.1946

One thing to note is: you need to be very careful with multidimensional arrays, to appropriately close all of the array constructs with parentheses,1956

and to make sure that you have commas in between each element of an array.1964

And so, in a multidimensional array...I'll say we just have an indexed array with an array and an integer in it--1970

so here we have a multidimensional array, and I'll set it to some value over here.1990

We have the first array, and it has its opening and closing parentheses; and then, within here, we have defined a second array1995

as the first item in this indexed array, and it is going to be an array as well; it's holding the integer 1.2004

If you notice, we have both the opening and closing parentheses with that; and that is very important.2012

The other thing that is important is that afterwards, we have a comma.2017

The comma is what is going to delineate that this array is the first element in this array, and then the integer 29 is the second element in this array.2021

Just be very careful with commas and parentheses when you work with these examples.2029

That ends today's lesson; thank you for watching Educator.com, and we look forward to seeing you at the next lesson.2035